Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace NewLine in .NET for Linux NewLine

I have a string in .NET defined like this:

var str = @"line 1
line 2
line 3";

I want to replace the Windows newline character for the Linux equivalent. (replacing \r\n for just \n).

I tried var str2 = str.replace(Environment.NewLine, "\n"); but the result is exactly the same string (str == str2 is true).

I guess that Windows replaces \n in strings with \r\n, effectively changing nothing.

I tried var str2 = str.replace(Environment.NewLine, '\n'); but gives compilation errors because one parameter is a string and the other is a char.

I even tried var str2 = str.replace(Environment.NewLine, '\n'.ToString()); but that did not work either.

Ideas?

like image 612
Fernando Avatar asked Jan 16 '14 02:01

Fernando


People also ask

How do you change to a new line in C#?

By using: \n – It prints new line. By using: \x0A or \xA (ASCII literal of \n) – It prints new line. By using: Console.

Does \n work in C#?

NewLine can be used in conjunction with language-specific newline support such as the escape characters '\r' and '\n' in Microsoft C# and C/C++, or vbCrLf in Microsoft Visual Basic.

How do you replace a line in a text file?

Open TextPad and the file you want to edit. Click Search and then Replace. In the Replace window, in the Find what section, type ^\n (caret, backslash 'n') and leave the Replace with section blank, unless you want to replace a blank line with other text.


2 Answers

str equals str2 after replacing because your original string does not contain \r\n in the first place.

See this fiddle.

The multiline string uses \n.

like image 191
dee-see Avatar answered Sep 21 '22 18:09

dee-see


It looks like it depends rather much on the tool you're working in. AND, on how you type or paste your literal.

A literal string with a line break seems to vary depending on the tool. One might worry that Environment.NewLine also might vary on the tool (besides the machine), but I haven't shown that yet.

AND you can also copy & paste a literal with line breaks that are different from what you get hitting "enter"/"return" in the tool.
Fun stuff! :)

When it counts like this, a safe bet across platforms would be to use those literals - such as "\r\n" in C# - in a single-line string and don't use line breaks in the editor.
Or else one could load from a file if that is more convenient for more data.


Drop this into VS, and then DotNetFiddle and see different results:

        var strMyString = @"start
        asdf
        end";
        byte[] ASCIIValues = System.Text.Encoding.ASCII.GetBytes(strMyString);
        string strENL = "";
        foreach (byte b in ASCIIValues)
        {
            strENL += b + " ";
        }
        Console.WriteLine("strMyString is as ascii: " + strENL);

        /////////
        Console.WriteLine("\n\n");
        var str2 = strMyString.Replace("\r","");
        Console.WriteLine("string 2 matches == " + (strMyString == str2).ToString());

        var str3 = strMyString.Replace("\n", "");
        Console.WriteLine("string 3 matches == " + (strMyString == str3).ToString());

        var str4 = strMyString.Replace(Environment.NewLine,"\n");
        Console.WriteLine("string 4 matches == " + (strMyString == str4).ToString());

        var str5 = strMyString.Replace(Environment.NewLine, "\r");
        Console.WriteLine("string 4 matches == " + (strMyString == str5).ToString());

        /////////
        Console.WriteLine("\n\n");
        strENL = "";
        ASCIIValues = System.Text.Encoding.ASCII.GetBytes(Environment.NewLine);

        foreach (byte b in ASCIIValues)
        {
            strENL += b;
        }
        Console.WriteLine("Environment.NewLine is as ascii: " + strENL);

        /////////


        Console.WriteLine("\n\n");
        Console.WriteLine((Environment.NewLine == "\n").ToString());
        Console.WriteLine((Environment.NewLine == "\r").ToString());
        Console.WriteLine((Environment.NewLine == "\r\n").ToString());      
like image 39
Mike M Avatar answered Sep 21 '22 18:09

Mike M