I was following this article
And I came up with this code:
string FileName = "C:\\test.txt";
using (StreamReader sr = new StreamReader(FileName, Encoding.Default))
{
string[] stringSeparators = new string[] { "\r\n" };
string text = sr.ReadToEnd();
string[] lines = text.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in lines)
{
Console.WriteLine(s);
}
}
Here is the sample text:
somet interesting text\n
some text that should be in the same line\r\n
some text should be in another line
Here is the output:
somet interesting text\r\n
some text that should be in the same line\r\n
some text should be in another line\r\n
But what I want is this:
somet interesting textsome text that should be in the same line\r\n
some text should be in another line\r\n
I think I should get this result but somehow I am missing something...
File.ReadAllLines
orFile.ReadLines
?Console.WriteLine()
will add the newline automatically after each string in your array. You want to either merge the lines together BEFORE your loop, or as one answer suggested, replace the\n
before youSplit
the text.\r\n
not\n
or\r
.\r\n
and\n
at the end but that you're forgetting that the lines actually contain already line-breaks. So even if you remove them you have three lines in the file.\r\n
? Add this line and see if it's what you expect:Console.WriteLine("lines.Length={0}", lines.Length);