Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NewLine replacement c#?

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");
like image 470
user1924391 Avatar asked Mar 05 '13 10:03

user1924391


People also ask

What is the character for new line in C?

In programming languages, such as C, Java, and Perl, the newline character is represented as a '\n' which is an escape sequence.

How do you find and replace in newline?

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.

Does \n make a new line?

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.

Does \n count as a character in C?

"\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.


2 Answers

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)?

like image 161
Tim Schmelter Avatar answered Oct 07 '22 19:10

Tim Schmelter


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");
like image 41
Raab Avatar answered Oct 07 '22 20:10

Raab