There is a string read from the outside. In this line, the text and no transitions to the new line. Instead, it contains \n handwritten, which denote these transitions.

So, I look at the line under the debugger, I see that in fact all \n are padded to \\n .

I start to carry out the replacement:

 string output = Regex.Replace(textToPrint, "\\n", "\n"); 

But it does not help, nothing happens. How to be? What am I doing wrong?

  • most likely you need to change to \r\n , and even better to use Environment.NewLine - Grundy
  • php is done like this ... $ str = 'some \ nsome \ nsome'; $ str = str_replace ('\ n', '\\\ n', $ str); - Boris Runs
  • one
    Is String.replace missing in .NET? - Vladimir Martyanov
  • "\ r \ n" - does not work. "string.replace" can not use (missing .Net). "Regex.Replace (textToPrint," \\ n "," \ n ")" also did not pass - Alerr
  • 2
    @Alerr how is it missing .NET if the tag is c # ?! - Vladimir Martyanov

2 answers 2

And you look at what the debugger shows, with understanding. It shows what the original C # line would look like, rather than the actual characters inside. But in reality there are two symbols everywhere in the text \ and n , there are no double bexleshes there.

Make it simple:

 // @"..." отключает интерпретацию `\` как escape character, так что в тексте будут именно // два символа \ и n, как и у вас string s = @"Как хороши, как свежи были розы\nДостаточно и половины дозы!"; // @"\n", как и раньше - строка из двух символов. // "\n" - "обыкновенная" строка, в ней компилятор заменит \n на символ перевода строки // с кодом 0x0A string decoded = s.Replace(@"\n", "\n"); // печатаем для проверки, видим разрыв строки Console.WriteLine(decoded); 

Check: http://ideone.com/J981fP

  • @Wiktor Stribiżew is right, and @"\n" will not work. You need exactly @"\\n" . Literally just checked in VS. - n01d
  • @ n01d: This contradicts the description of the vehicle and the trial code. Try it yourself to trace the code that I gave, and make sure. - VladD
  • yes, wrong. Regex.Replace and the String.Replace method work differently. Regex.Replace will correctly find in your string precisely by @"\\n" . - n01d
  • one
    @ n01d: EMNIP, \ in Regex.Replace is re-interpreted, yes. - VladD

under the debugger, I see that in fact all \n are padded to \\n .

This means that in the text instead of a line feed there are backslashes with the letter n after them.

For a regular expression to find such combinations of characters, you must use

 @"\\n" 

Here \\ means 2 literal characters \ , i.e. pattern for finding backslashes.

Demo :

 var textToPrint = @"Текст 1\nТекст2"; Console.WriteLine(textToPrint); // => Текст 1\nТекст2 var output = Regex.Replace(textToPrint, @"\\n", "\n"); Console.WriteLine(output); // => Текст 1 //Текст2 
  • um, @ just allows you to use special characters without escaping and do not need to escape \` Extras, That is: @"\n" == "\\n" - Grundy
  • @Grundy: the point is that there are \ characters in the text. It is necessary to find substrings of 2 characters: \ + n -> @"\\n" . You can try Regex.Unescape(textToPrint) , but this may not work. - Wiktor Stribiżew
  • @ n01d, Actually, no: the words under the debugger, I see that the line contains the \ character, which under the debugger is shown as \\ so when using @ you need to look for exactly @"\n" - Grundy
  • @ WiktorStribiżew, here is the error \ + n -> @"\n" or "\\n" - Grundy
  • "\\n" will find the LF character . - Wiktor Stribiżew