2014-01-30 4 views
0

문자열이있는 info.txt라는 텍스트 파일이 있습니다.부분 문자열 발생 횟수

info.txt

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

내 출력은 기본적으로 내가 03 "로 시작하는 모든 문자열의 수를 얻어야한다

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

new.txt new.txt

출력에 있어야합니다 부분 문자열 "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

처럼 내 코드를 썼다. 하위 문자열'01' 전에 수를 인쇄하려고하지만 예상 출력에서 ​​'05'전에 인쇄 할 것입니다. –

+0

파일의 첫 번째 줄이 05로 시작되면 즉시 카운터를 씁니다. 기본적으로, 당신은 BEHIND보다는 AHEAD를 읽을 필요가 있습니다. 즉, 카운터를 작성하기로 결정하기 전에 NEXT 라인이 무엇인지 알 필요가 있습니다. –

+0

제공된 출력에 따르면 "05"로 시작하는 문자열이 나타날 때마다 카운트를 인쇄하고 파일의 끝에 논리가있는 것처럼 보입니다. 이 올바른지? – etaiso

답변

2

당신의 while loop 그 같은 시도 할 수 있습니다 : 읽기 AHEAD 구현을 사용

... 
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

가 여기 코드입니다. 그냥 루프의 끝나기 전에, 당신은 카운터 도움이들 r에 모든

  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."); 
     } 
0

감사를 당신에게 출력은 (파일의 끝) null의 경우, 또는이 "05"로 시작하는 경우, 다음 줄을 읽기 및 재설정 , 당신의 모든 입력에서, 나는 마침내 내가 필요한 출력을 얻었다.

나는 당신이 얻으려고 노력 숫자의 논리를 얻을 수없는 두 번하고 여전히 샘플을 읽게이

 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(); 
     }