I have a windows phone application. In the application I have a textbox whose acceptsreturn property is set to true.
What I want to do is create a string from the textbox and replace the new lines with a specific character, something like "NL"
I've tried the following but none of them worked.
string myString = myTextBox.Text.Replace(Environment.NewLine,"NL");
string myString = myTextBox.Text.Replace("\n","NL");
In programming languages, such as C, Java, and Perl, the newline character is represented as a '\n' which is an escape sequence.
On the keyboard, press Ctrl + H to open the Find and Replace dialog box, with the Replace tab active. On the Replace tab, click in the Find What box. On the keyboard, press Ctrl + J to enter the line break character.
The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.
"\n" is a character. char[] to represent a string needs "0" to mark the end of the string. In order to do that, char array requires one more element to store "0" but it is not counted as a string size. So in this case, char array size is 2, string size is 1.
I'm not familiar with windows phone(or silverlight), but try to split with \r
instead:
string myString = myTextBox.Text.Replace("\r","NL");
Why does a Silverlight TextBox use \r for a newline instead of Environment.Newline (\r\n)?
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With