2

I am loading a string from my database which among other things contains line breaks (\r\n). However, this isn't being rendered as a new line but instead as \r\n.

If I type it directly in instead of loading it from a string, it works just fine but I need to be able to load it from a string.

Any ideas?

Edit: Upon closer inspection, it looks like the string is being returned as:

Changed test7\\r\\nChanged test8\\r\\nChanged test9Changed test7

From the database.

I tried running a .Replace(@"\\", @"\") on it but this had no effect at all. Any ideas?

TheGateKeeper
  • 4,420
  • 19
  • 66
  • 101

5 Answers5

5

What about this?

string yourString="something\r\n..somethingMore";
yourString=yourString.replace("\r\n",Environment.NewLine);
yourTextBox.Text=yourString;

Does this solution meet your requirements?

competent_tech
  • 44,465
  • 11
  • 90
  • 113
Andrea Scarcella
  • 3,233
  • 2
  • 22
  • 26
2

Call .Replace(@"\r", "\r").Replace(@"\n", "\n")

or just .Replace(@"\r\n", "\r\n")

Balazs Tihanyi
  • 6,659
  • 5
  • 23
  • 24
1

Is the Multiline property on your TextBox set to true?

MarkF
  • 11
  • 1
1

When you call your .Replace function, you're appending your strings with @. By doing so, your @"\\" gets converted to "\\\\" and your @"\" gets converted to "\\".

Try running .Replace("\\", "\"), and it should work.

K Mehta
  • 10,323
  • 4
  • 46
  • 76
1

This won't work .Replace("\\", "\") because it sees the backslash as an escape, but strangely, this doesn't work when it should: .Replace(@"\\", @"\")

The one that I got to work is this:

.Replace("\\r\\n", "\r\n");
Brad Rem
  • 6,036
  • 2
  • 25
  • 50