18

Possible Duplicate:
Easiest way to split a string on newlines in .net?

I'm trying to read out and interpret a string line per line. I took a look at the StringReader class, but I need to find out when I'm on the last line. Here's some pseudocode of what I'm trying to accomplish:

while (!stringReaderObject.atEndOfStream()) {
    interpret(stringReaderObject.readLine());
}

Does somebody know how to do this?

Thanks!

Yvan

2
  • 2
    Not really a duplicate... both questions have different solutions.
    – AStopher
    Dec 13, 2016 at 16:07
  • Here's the solution that worked for me: private static IEnumerable<string> ReadAllLines(string multiLineString) { if (string.IsNullOrEmpty(multiLineString)) yield break; // Nothing to do using (var reader = new StringReader(multiLineString)) { string line; while ((line = reader.ReadLine()) is object) { yield return line; } } }
    – Mark Moore
    Mar 10, 2021 at 21:16

6 Answers 6

76

If you are reading it in from a file, it is easier to do just do:

foreach(var myString in File.ReadAllLines(pathToFile))
    interpret(myString);

If you are getting the string from somewhere else (a web service class or the like) it is simpler to just split the string:

foreach(var myString in entireString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
    interpret(myString);
8
  • what if his string has \r\n new lines and he's on Unix?
    – NG.
    Oct 21, 2010 at 17:05
  • 1
    @SB then I'm sure he would have added the "mono" and "interop" tags ;-) Oct 21, 2010 at 17:08
  • 3
    Worth mentioning is File.ReadLines (.Net 4.0) which returns a IEnumerable<string> so you get lines while reading the file which is better in two ways: 1. You don't have the whole line-array in memory if you don't need it. 2. ReadAllLines reads the whole file before you get a single line where ReadLines will give you the first line as soon as it is read. (more responsive). Because he is calling a method on each line which interprets it, he properly wants ReadLines and not ReadAllLines. Oct 21, 2010 at 17:44
  • Doesn't work for me. The best overloaded method match for 'string.Split(params char[])' has some invalid arguments Oct 2, 2012 at 9:47
  • @ColonelPanic, sorry the example was flawed. I have updated it. Hope it helps. Oct 2, 2012 at 9:53
4

Check for null when you do a readLine - see the docs.

3

Like this:

string line;
while (null != (line = reader.ReadLine()) {
    Process(line);
}
7
  • 1
    No. Like this: while(true) { string line = reader.ReadLine(); if(line == null) { break; } Process(line); }. That single line of code is doing too much.
    – jason
    Oct 21, 2010 at 17:04
  • 1
    @Jason - not really. Also it's the canonical pattern of use in every example I've ever seen.
    – Kev
    Oct 21, 2010 at 17:10
  • @Kev: It's like a bad habit passed from generation to generation.
    – jason
    Oct 21, 2010 at 17:34
  • 1
    @Jason - no it's not. It's succinct and entirely readable. You're taking single responsibility way too far.
    – Kev
    Oct 21, 2010 at 18:25
  • 1
    @Kev: Since this has nothing to do with SRP, I'm not. This is mostly about readability, and to a certain extent maintainability. Succinct code is not necessarily readable code.
    – jason
    Oct 21, 2010 at 18:29
2

The StreamReader class has an EndOfStream property. Have you looked into using that?

2

I am not sure where you are reading from. If it is from the Console, you can check for the end of inputs string like "quit" to denote end of input

String input;
while((input = reader.readLine()) != "quit"){
   // do something
}
1

Dismissile is correct:

http://msdn.microsoft.com/en-us/library/system.io.streamreader.endofstream.aspx

while (!reader.EndOfStream) 
{ 
    string line = reader.ReadLine(); 
} 
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.