2009-09-30 9 views

Odpowiedz

116

Proponuję przy użyciu kombinacji StringReader i moją LineReader klasę, która jest częścią z MiscUtil, ale również dostępny w this StackOverflow answer - można łatwo skopiować tę klasę do własnego projektu użytkowego. Można by użyć go w ten sposób:

string text = @"First line 
second line 
third line"; 

foreach (string line in new LineReader(() => new StringReader(text))) 
{ 
    Console.WriteLine(line); 
} 

pętli na wszystkich liniach w ciele danych String (czy to jest plik lub cokolwiek) jest tak powszechne, że nie powinno wymagać kod wywołujący być na testowanie zerowy itp :) Mimo, że jeśli zrobić chcą zrobić ręczną pętlę, to jest forma, że ​​zazwyczaj wolą nad Fredrik na:

using (StringReader reader = new StringReader(input)) 
{ 
    string line; 
    while ((line = reader.ReadLine()) != null) 
    { 
     // Do something with the line 
    } 
} 

w ten sposób można mieć tylko do testowania nieważności raz, nie musisz też myśleć o pętli do/while (która z jakiegoś powodu wymaga ode mnie więcej wysiłku, niż pętli prostej).

53

Można użyć StringReader czytać wiersz naraz:

using (StringReader reader = new StringReader(input)) 
{ 
    string line = string.Empty; 
    do 
    { 
     line = reader.ReadLine(); 
     if (line != null) 
     { 
      // do something with the line 
     } 

    } while (line != null); 
} 
5

z MSDN dla StringReader

string textReaderText = "TextReader is the abstract base " + 
     "class of StreamReader and StringReader, which read " + 
     "characters from streams and strings, respectively.\n\n" + 

     "Create an instance of TextReader to open a text file " + 
     "for reading a specified range of characters, or to " + 
     "create a reader based on an existing stream.\n\n" + 

     "You can also use an instance of TextReader to read " + 
     "text from a custom backing store using the same " + 
     "APIs you would use for a string or a stream.\n\n"; 

    Console.WriteLine("Original text:\n\n{0}", textReaderText); 

    // From textReaderText, create a continuous paragraph 
    // with two spaces between each sentence. 
    string aLine, aParagraph = null; 
    StringReader strReader = new StringReader(textReaderText); 
    while(true) 
    { 
     aLine = strReader.ReadLine(); 
     if(aLine != null) 
     { 
      aParagraph = aParagraph + aLine + " "; 
     } 
     else 
     { 
      aParagraph = aParagraph + "\n"; 
      break; 
     } 
    } 
    Console.WriteLine("Modified text:\n\n{0}", aParagraph); 
1

Oto krótki fragment kodu, który znajdzie pierwszy niepusty wiersz w ciąg:

string line1; 
while (
    ((line1 = sr.ReadLine()) != null) && 
    ((line1 = line1.Trim()).Length == 0) 
) 
{ /* Do nothing - just trying to find first non-empty line*/ } 

if(line1 == null){ /* Error - no non-empty lines in string */ } 
0

wiem, że to zostało odebrane, ale chciałbym dodać własną odpowiedź:

using (var reader = new StringReader(multiLineString)) 
{ 
    for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) 
    { 
     // Do something with the line 
    } 
} 
Powiązane problemy