计算子串出现次数

时间:2014-01-30 07:33:37

标签: c#

我有一个名为info.txt的文本文件,其中包含字符串。

info.txt

05331
02555
03211
05222
04321
02387
03444
03127
05117
03680
03881
01579
03111

我的输出应该是new.txt

输出new.txt

05331
02555
03211
1
05222
04321
02387
03444
03127
2
05117
03680
03881
01579
03111
3

基本上我应该得到以“03”开头的所有字符串的计数,并在子字符串“01”之前打印计数

       try
       {
           String line;
           Int counter =0;
           StreamReader sr = new StreamReader("C:\\Files\\gamenam.txt");
           StreamWriter sw = new StreamWriter("C:\\Files\\gamenam_1.txt");

           while ((line = sr.ReadLine())!= null)
           {
               if (line.substring(0,2) == "05")
               {
                   sw.Write(counter.ToString());
                   counter =0;
               }
               If (line.subString(0,2) =="03")
               {
                   //loop
                   counter++;
               }

               sw.WriteLine(line);
           }

           sr.Close();
           sw.Close();
       }
       catch (Exception e)
       {
           Console.WriteLine("Exception: " + e.Message);
       }
       finally
       {
           Console.WriteLine("Exception finally block.");
       }
   }

写完我的代码后,我才能得到。

0
05331
02555
03211
1
05222
04321
02387
03444
03127
2
05117
03680
03881
01579
03111

第一行的0不应该是因为我之前没有蜇,并且没有最后一次计数。

请帮帮我们。

3 个答案:

答案 0 :(得分:2)

而不是你的while loop你可以尝试类似的东西:

...
Boolean isFirstLine = true;

while ((line = sr.ReadLine()) != null) {
  // If line starts with "05" we should print out counter 
  // (that is number of "03" started lines)
  // unless it is the first line in the file
  if (line.StartsWith("05")) {
    if (!isFirstLine)
      sw.WriteLine(counter.ToString()); 

    sw.WriteLine(line); 
    counter = 0;  
    isFirstLine = false; 

    continue;
  }

  sw.WriteLine(line);

  if (line.StartsWith("03")) 
    counter += 1;

  // We should also print out counter after the last file line
  // if, say, counter > 0 
  if (sr.Peek() < 0) // <- No next line
    if (counter > 0)
      sw.WriteLine(counter.ToString());

  isFirstLine = false;
}
...

答案 1 :(得分:1)

此处使用READ AHEAD实现的代码。在循环结束之前,您将读取下一行,如果它为null(文件结尾)或者以&#34; 05&#34;然后你输出并重置计数器

            try
            {
                int counter = 0;
                //Pass the file path and name to the StreamReader constructer
                StreamReader sr = new StreamReader("gamenam.txt");
                //Pass the file path and name to the StreamReader constructer
                StreamWriter sw = new StreamWriter("gamenam_1.txt");


                string line = sr.ReadLine();
                while (line != null)
                {
                    if (line.Substring(0, 2) == "03")
                    {
                        counter++;
                    }

                    sw.WriteLine(line);

                    line = sr.ReadLine();
                    if ((line == null) || (line.StartsWith("05")))
                    {
                        sw.WriteLine(counter.ToString());
                        counter = 0;
                    }
                }

                //Close
                sr.Close();
                sw.Close();
            }
            //Catching exception
            catch (Exception e)
            {
                //Exception Message
                Console.WriteLine("Exception: " + e.Message);
            }
        }
        finally
        {
            Console.WriteLine("Exception finally block.");
        }

答案 2 :(得分:0)

感谢所有你帮助的人,从你输入的所有内容中,我终于得到了我需要的输出。

我写了这样的代码

       String line;
       int counter = 0;
       Boolean isFirstLine = true;
       try
       {
           StreamReader sr = new StreamReader("C:\\Files\\gamenam.txt");
           StreamWriter sw = new StreamWriter("C:\\Files\\gamenam_1.txt");

           while ((line = sr.ReadLine()) != null)
           {
               if (line.Substring(0, 2) == "01")
               {
                   if (!isFirstLine)
                   {
                       sw.WriteLine(counter.ToString());
                       counter = 0;  
                   }
               }
               if (line.Substring(0, 2) == "05")
               {
                   counter++;
               }
               sw.WriteLine(line);
               if (sr.Peek() < 0)
               {
                   sw.Write(counter.ToString());
               }
               isFirstLine = false;
           }
           sr.Close();
           sw.Close();
       }
相关问题