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

Upvotes: 3

Views: 7825

Answers (2)

Nikita Ignatov
Nikita Ignatov

Reputation: 7163

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.

Upvotes: 5

Filip Skakun
Filip Skakun

Reputation: 31724

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

Upvotes: 1

Related Questions