21

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. Commented Dec 13, 2016 at 16:07
  • 1
    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; } } } Commented Mar 10, 2021 at 21:16

6 Answers 6

80

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);
Sign up to request clarification or add additional context in comments.

8 Comments

what if his string has \r\n new lines and he's on Unix?
@SB then I'm sure he would have added the "mono" and "interop" tags ;-)
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.
Doesn't work for me. The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
@ColonelPanic, sorry the example was flawed. I have updated it. Hope it helps.
|
5

Like this:

string line;
while (null != (line = reader.ReadLine()) {
    Process(line);
}

8 Comments

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 - not really. Also it's the canonical pattern of use in every example I've ever seen.
@Jason - no it's not. It's succinct and entirely readable. You're taking single responsibility way too far.
@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 - there's nothing hard to read about that code. Hell, I'm an ex-VB/Clipper programmer and could understand that when I started out with .NET. That extra if() is just a distraction.
|
4

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

Comments

3

Dismissile is correct:

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

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

1 Comment

EndOfStream is a property of StreamReader. This question is asking about StringReader, which has no such property.
1

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

Comments

1

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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.