Search code examples
c#replacetextboxnewline

c# replace "\n" text with newline in a textbox


I have some text (for example "o\nour first place, and this\n\n13") and I want that for each "\n" string founded into the text this must be replaced with newline...

output for the example will be:

o our first place, and this

13 How can I make? textbox is multiline

the code is

string text_str = txtbox.Text;
text_str .Replace("(?<!\r)\n", "\r\n");
txtbox.Clear();
txtbox.Text = text_str;

Solution

  • I think you're looking for something like this:

    string text_str = txtbox.Text;
    text_str = text_str.Replace("\\n", "\r\n");
    txtbox.Clear();
    txtbox.Text = text_str;
    

    Although that's a really round about way of doing things. This will accomplish the same thing:

    txtbox.Text = txtbox.Text.Replace("\\n", "\r\n");