C #: multiline line concatenation

What is the best way to scroll through each line of a multi-line line without using a much larger amount of memory (for example, without splitting it into an array)?

+64
c # loops
Sep 30 '09 at 19:31
source share
5 answers

I suggest using a combination of StringReader and my LineReader class, which is part of MiscUtil , but is also available in https://stackoverflow.com/a/166269/167/ - you can easily copy only this class into your own utility project. You would use it as follows:

 string text = @"First line second line third line"; foreach (string line in new LineReader(() => new StringReader(text))) { Console.WriteLine(line); } 

Looping over all the lines in the string data (whether it be a file or something else) is so common that it does not require the calling code to test zero, etc. :) Having said that, if you want to make a manual loop, this is a form, which I usually prefer over Fredrik's:

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

That way, you only need to check the nullity value once, and you don’t need to think about the do / while loop (which for some reason always takes more effort to read than the direct while loop).

+113
Sep 30 '09 at 19:41
source share
β€” -

You can use StringReader to read a string at a time:

 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); } 
+53
Sep 30 '09 at 19:36
source share

from MSDN to 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); 
+5
Sep 30 '09 at 19:37
source share

Here is a quick code snippet that will find the first non-empty line in the line:

 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 */ } 
+1
Oct 11 '10 at
source share

I know this was answered, but I would like to add my own answer:

 using (var reader = new StringReader(multiLineString)) { for (string line = reader.ReadLine(); line != null; line = reader.ReadLine()) { // Do something with the line } } 
0
Feb 06 '17 at 8:57
source share



All Articles