Take the 2-minute tour ×

I am having a weird problem. I read a text file then I split it using \n as a delimiter. I noticed that the split lines contain '\r' at the end. So I am trying to remove it, I tried doing so using String.Replace but without luck. Here is my code:

    string fileOutput = File.ReadAllText(location);
    foreach (string line in fileOutput.Split('\n'))
        string cleanLine = line;
        if (cleanLine.Contains("\r")) cleanLine.Replace("\r", "");

EDIT: After staring at the code for 1 min, I found that I didn't assign the replaced value to the original string.

cleanLine.Replace("\r",""); //assigns a value to nothing

I should have assigned cleanLine to cleanLine.Replace();

cleanLine = cleanLine.Replace("\r","");
share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

I'm pretty sure running fileOutput.Replace("\r","") would be better than calling a replace for each of the strings attained after the split. It might fix your problem as well.

share|improve this answer
Actually I don't know how didn't I do that. I need to take some rest. Thanks Vlad, it fixed the problem =] –  deadlock Jan 23 '11 at 4:37
Thank you. A rest is always better than a couple of hours of chasing bugs you wouldn't have made in the first place ;) P.S. I would be happy if you marked my answer as the one fixing the problem. –  Vlad Jan 23 '11 at 4:43
add comment

If you call:

string[] lines = File.ReadAllLines(location);

it will remove both the '\r' and '\n' from each line for you.

share|improve this answer
Arghhh, just said that too :) +1 –  leppie Jan 23 '11 at 4:47
That looks like the best way to go. –  deadlock Jan 23 '11 at 5:03
add comment

I think you're misunderstanding how strings work in C#, and how text files generally work on Windows.

On Windows a new line is delimited by Carriage Return (\r), and Line Feed (\n)? So, this is "normal".

Secondly, in C# strings are immutable.

if (cleanLine.Contains("\r")) cleanLine.Replace("\r", ""); does not assign the 'cleaned' value.

You would need to use cleanLine = cleanLine.Replace("\r","");

Thirdly, from a style perspective in C# - you should place code blocks on another line, and surround them with brackets. It's valid syntax not to, but it reduces readability.

As a last thing - you could simply open the text file and call ReadLine rather than needing to do string replacement as you're doing now.

share|improve this answer
Yes of course I know that I should have used cleanLine=cleanLine.Replace(); And I also know that it was my main problem and caused my code not to work. I just missed it as I haven't slept for much much time. Thanks for the constructive comment Will =] +1 –  deadlock Jan 23 '11 at 4:59
add comment

In Windows, the line indicator is actually \r\n, so your best bet may be to do an initial replace of "\r\n" with some other character that isn't found elsewhere in the file (like maybe "|"), and then split on the replaced character. Otherwise, if you just replace "\r", you may end up replacing a "\r" that isn't actually at the end of a line (although this is pretty doubtful).

How are you reading the file in the first place? The .Net TextReader class has a ReadLine() method, the use of which would let you avoid this particular problem entirely.

share|improve this answer
I guess there is no need for that complications. I was missing something very simple, I didn't assign the replaced string to the original one and that's why it didn't work. Thanks for your comment though =] –  deadlock Jan 23 '11 at 5:01
add comment

As others have suggested, use File.ReadAllLines() instead. It will split on \n as well as \r\n. Your sample can be rewritten as:

foreach (string line in File.ReadAllLines(location))
share|improve this answer
So why add a duplicate answer? –  leppie Jan 23 '11 at 4:54
It's not a duplicate answer, it explains why it is preferable to use ReadAllLines(), and it includes a concise code sample. –  Quick Joe Smith Jan 23 '11 at 4:57
The other answer explains that too. Sample code is redundant. SettingsManager.proxies = new List<string>( File.ReadAllLines(location)) would be better. Or AddRange if property is read-only. –  leppie Jan 23 '11 at 5:04
add comment

Your Answer


