User13839404 User13839404 - 4 days ago 4
C# Question

How to read text file by particular line separator character?

Reading a text file using streamreader.

using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
string line = sr.ReadLine();
}


I want to force that line delimiter should be
\n
not
\r
. So how can i do that?

Answer
string text = sr.ReadToEnd();
string[] lines = text.Split('\r');
foreach(string s in lines)
{
   // Consume
}
Comments