2009-09-30 21 views

risposta

116

Io suggerisco di usare una combinazione di StringReader e mia classe LineReader, che fa parte di MiscUtil ma disponibile anche in this StackOverflow answer - è possibile copiare facilmente solo quella classe nel proprio progetto di utilità. Usereste in questo modo:

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

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

Looping su tutte le linee in un corpo di dati stringa (se questo è un file o altro) è così comune che non dovrebbe richiedere il codice chiamante essere test per nullo ecc :) detto questo, se si fai vuole fare un ciclo manuale, questa è la forma che io di solito preferisco più di Fredrik di:

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

in questo modo si hanno solo per testare di nullità, una volta, e non devi neanche pensare a un ciclo do/while (che per qualche ragione mi richiede sempre più sforzo di lettura rispetto a un ciclo while).

53

è possibile utilizzare un StringReader per leggere una riga alla volta:

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

da MSDN per 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

Ecco un frammento di codice rapido che troverà la prima riga non vuota in una stringa:

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

So che questo è stato risposto, ma mi piacerebbe aggiungere la mia risposta:

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