Reading a string line per line in C#

Reading a string line per line in C#

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);

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

Reading a string line per line in C#

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

Leave a Reply

Your email address will not be published. Required fields are marked *