Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

line break in string in c#

I'm loading a text from the resource loader which includes '\n' to indicate a new line. A textblock takes this text and displays it, but I can't see the line break? I'm also trying to replace every '\n' by Environment.NewLine, but nothing happens. What can I do?

Here is a little bit code:

        TextBlock text = new TextBlock();
        text.FontSize = 18;

        var str = loader.GetString("AboutText");
        //str.Replace("\n", Environment.NewLine);

        text.Text = str;
        text.TextAlignment = TextAlignment.Justify;
        text.TextWrapping = TextWrapping.Wrap;
        text.Margin = new Thickness(10, 10, 10, 10);
like image 717
Thomas Sebastian Jensen Avatar asked Dec 04 '25 12:12

Thomas Sebastian Jensen


2 Answers

It looks like the Resource file is escaping \n to \\n, this means that there are basically 2 solutions to solve this.

you can either

var str = Regex.Unescape(loader.GetString("AboutText")); 

or in your resx file you can replace \n with normal break line by pressing Shift Enter.

like image 128
Nikita Ignatov Avatar answered Dec 06 '25 23:12

Nikita Ignatov


Try "\r\n". It works for me.

    TextBlock text = new TextBlock();
    text.FontSize = 18;

    var str = "Hello\r\nWorld";

    text.Text = str;
    text.TextAlignment = TextAlignment.Justify;
    text.TextWrapping = TextWrapping.Wrap;
    text.Margin = new Thickness(10, 10, 10, 10);
    layoutRoot.Children.Add(text);
like image 27
Filip Skakun Avatar answered Dec 06 '25 22:12

Filip Skakun