Removing \ r from lines read from a file

I have a strange problem. I read a text file and then split it using \ n as a delimiter. I noticed that the dividing lines contain "\ r" at the end. So I'm trying to remove it, I tried to do it with String.Replace, but with no luck. Here is my code:

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

EDIT: after looking at the code for 1 minute, I found that I did not assign the replaced value to the original string.

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

I had to assign cleanLine to cleanLine.Replace ();

cleanLine = cleanLine.Replace("\r","");
+3
source share
5 answers

I am sure that starting fileOutput.Replace("\r","")will be better than calling a replacement for each of the lines reached after the split. It may also solve your problem.

+3
source

If you call:

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

"\ r", "\n" .

+7

, , #, Windows.

Windows (\ r) (\n)? , "".

-, # .

if (cleanLine.Contains("\r")) cleanLine.Replace("\r", ""); "" .

cleanLine = cleanLine.Replace("\r","");

-, # - . , .

: ReadLine, , .

+2

Windows \r\n, "\ r\n" , (, | "), . , " \ r ", " \ r", ( ).

? .Net TextReader ReadLine(), .

+1

, File.ReadAllLines(). \n, \r\n. :

SettingsManager.proxies.Clear();
foreach (string line in File.ReadAllLines(location))
    SettingsManager.proxies.Add(line);
+1

Source: https://habr.com/ru/post/1787243/


All Articles