Replacing NewLine C #?

I have an application for a Windows phone. In the application, I have a text field whose acceptsreturn property is true.

What I want to do is create a line from a text field and replace the new lines with a specific character, something like "NL"

I tried the following, but none of them worked.

 string myString = myTextBox.Text.Replace(Environment.NewLine,"NL"); string myString = myTextBox.Text.Replace("\n","NL"); 
+7
source share
4 answers

I am not familiar with a Windows phone (or silverlight), but instead try splitting with \r :

 string myString = myTextBox.Text.Replace("\r","NL"); 

Why does Silverlight TextBox use \ r for a new line instead of Environment.Newline (\ r \ n)?

+8
source

Consider replacing different types of line breaks to handle all the possibilities.

 string myString myTextBox.Replace("\r\n", "NL").Replace("\n", "NL").Replace("\r", "NL"); 
+3
source

Use this code

 var myString = myTextBox.Text.Replace("\r","NL"); 

This is for compatibility with all operating systems.

+1
source

A question with a similar topic was a rather elegant answer that might seem interesting to use.

 using System.Text.RegularExpressions; myTextBox.Text = Regex.Replace(myTextBox.Text, @"\r(?!\n)|(?<!\r)\n", "NL"); 
+1
source

All Articles