程序实现这样的功能:
查看某个字符串是否在文本文件中重复出现,如重复出现,则删除该字符吕并把该字符串前后部分(不为空串的话)拆分为两行。
目前反复测试,程序正确,功能上没问题。但对于大文本文件,运行时间比较慢长,不知是否还可以进行优化?1、把文本文件读入到一个ArrayList中:
            arrPhrase = new ArrayList();
           StreamReader sr = new StreamReader(txtInput.Text, Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Trim().Length>1)
                    arrPhrase.Add(line.Trim());
            }
            sr.Close();
2、编写一个查询某一行中某子串出现次数的方法:
        private int getCounts(string str1,string str2){   //返回str1在str2中出现的次数
             int i1=str1.Length;
             return ((str2.Length - str2.Replace(str1, "").Length)/i1);
         }
3、主体程序,思路是先循环查询整个文本文件读入后的ArrayList,只要发现出现次数大于等于二,则进入第二步,再循环对整个文本文件读入后的ArrayList进行替换拆分。
                        iCount=0;
                        for (int j = 0; j < arrPhrase.Count; j++)
                        {
                            iCount =iCount+ getCounts(“重复则删除的字符串”, arrPhrase[j].ToString());
                            if (iCount > 1)
                            {
                                isMatch = true;
                                break;
                            }
                        }
                        if (isMatch)
                        {
                             for (int j = 0; j < arrPhrase.Count; j++)
                            {
                                string[] sArray = arrPhrase[j].ToString().Split(new[]{“重复则删除的字符串”}, StringSplitOptions.None);
                                Application.DoEvents();
                                if (sArray.Length>1){
                                    foreach (string strX in sArray){
                                        if (strX.Length>1)
                                            arrPhrase.Add(strX);                                    }
                                }

解决方案 »

  1.   

    在第一步读入的时候,直接用split函数,后面两步都能省了。
    减少了2个循环,应该就会快很多。
      

  2.   

    写了一个,肯定不是最快的,欢迎大家提出指正
    控制台程序static List<string> getList(List<string> p_dataList,string splitKey)
    {
        List<string> addedList = new List<string>(); //存放分离出来的子字符串    int keyCount = 0; //记录分割字符串个数
        int arrLength = p_dataList.Count;
        string[] tempArr; //临时存放每行拆分出来的子字符串    for(int i=0;i<arrLength;i++)
        {
            tempArr = Regex.Split(p_dataList[i], splitKey);
            if (tempArr.Length > 1)
            {
                keyCount++;
                if (keyCount > 1)
                {
                    for(int j=1;j<tempArr.Length;j++)
                    {
                        addedList.Add(tempArr[j]);
                    }                p_dataList[i] = tempArr[0];            }        }
        }    p_dataList = p_dataList.Union(addedList).ToList();    return p_dataList;}static void Main(string[] args)
    {
        List<string> dataList = new List<string>();
        dataList = System.IO.File.ReadAllLines(@"D:\123.txt").ToList(); //根据你的习惯,可以按照你的写法    System.Diagnostics.Stopwatch sw = new Stopwatch();
        sw.Start();
        dataList = getList(dataList, "ABC");
        sw.Stop();    //输出分割后的list 
     
    }测试用的123.txtDKFKDkdfjdABCifie284dkjf
    kfjdjfdfieryi
    kdjfeidoss8593sa
    DKJFOEI949F
    MNXBCABCksjdiw3u49ABCeeeeeeeeee
    q2ios9idfjs9e
    djfosmdfsfs,cmvns
    lxjowie9DKD
    KDJFLSJFSDFPWOOIFIDJF09U192i0ujkk0ww-
    kdjfius032
    lkdsKDJFSJFDKFS
    DSLKFLLDABCO29839jdfksjkdfsieeirrABC93898ABCjnxnqjnxnviwmvABCDFJLSJFS
    KDSKDOoweriwrxlkpei
    oiwroiwpsmkm nnvq2u93uskisq
    ksjfksjfsoOIOSEJKFOSkjfkis9924998
    KDDFS49udjgoe29I0EUXVP390SJFLKLpoii394oj
    ksjfksjowur02ijfjfs
    111111111111888ajffffffffff
     
    输出
      

  3.   

    页面加上using System.Text.RegularExpressions;
      

  4.   

                var res = new List<string>();
                foreach(var s in File.ReadAllLines("123.txt"))
                {
                    res.AddRange(s.Split(new string[]{"ABC"}, StringSplitOptions.RemoveEmptyEntries));
                }
                Console.WriteLine(string.Join("\r\n",res));
      

  5.   

    更正下:昨晚写的有点问题static List<string> getList(List<string> p_dataList,string splitKey)
    {
        int keyCount = 0; //记录分割字符串个数
        int arrLength = p_dataList.Count;
        string[] tempArr; //临时存放每行拆分出来的子字符串    for(int i=0;i<arrLength;i++)
        {
            tempArr = p_dataList[i].Split(new string[] { "ABC" }, StringSplitOptions.None);
            if (tempArr.Length > 1)
            {
                keyCount++;
                if (keyCount > 1)
                {
                    for(int j=1;j<tempArr.Length;j++)
                    {
                        p_dataList.Add(tempArr[j]);                }                p_dataList[i] = tempArr[0];            }        }
        }    return p_dataList;}