Replace with \ does not work

2

I'm trying to make a Replace() but it does not work. I need to change from \ to \ .

"\r\n".Replace(@"\", "\");

It only returns me: "\r\n" and not "\r\n" .

    
asked by anonymous 04.12.2017 / 19:59

1 answer

1

First, I do not think I need to do this, but I'm just speculating. I think there is another problem that is giving a wrong solution.

It's not working because you're changing something for yourself. @"\" is exactly the same as "\" .

How it works:

var texto = @"\r\n".Replace(@"\", @"\");

@ causes the \ escape character to be considered a normal character, between other things. When you use \ without @ the backslash is an escape and when you use two backslashes it means that you should consider the bar as a normal character.

Do not forget to save the information somewhere (like I did) or immediately use the expression.

using static System.Console;

public class Program {
    public static void Main() {
        var texto = @"É necessário Cadastrar o(s) seguinte(s) Parâmetro(s):SENHA_FTP\r\n";
        WriteLine(texto.Replace(@"\", @"\"));
    }
}

See running on .NET Fiddle . And < strong> Coding Ground . Also put it in GitHub for future reference .

    
04.12.2017 / 20:01