0

I am displaying a message to the user like so:

Messagebox.Show("I am a \"Test\" Message.\r\nAm I working?");

and that works fine.

But now I add my message to a Sql database as an nvarchar(max) and retrive it from the database but now my messagebox shows everything including the \"

so when I do:

Messagebox.Show(databaseString)

It displays the literal string:

I am a \"Test\" Message.\r\nAm I working?

Instead of:

I am a "Test" Message.

Am I working?"

How do I get rid of the \" and \r\n when I get my string from a database? Do I need to encode it?

PS. I don't want to use String.Replace as there could be more uses in the future i.e. \t

JKennedy
  • 18,150
  • 17
  • 114
  • 198

2 Answers2

1

What if you store it

string str = @"I am a ""Test"" Message." + Environment.NewLine + "Am I working?";
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • So I store the string `@"I am a ""Test"" Message." + Environment.NewLine + "Am I working?"` in my database? Are you sure that would work? Ive got a feeling it wouldn't work – JKennedy Aug 14 '15 at 12:05
  • @user1, not tested though but it should work. Idea is to use verbatim string literal and use framework provided newline syntax rather than hardcoding it. Give it a try and see. – Rahul Aug 14 '15 at 12:11
0

The solution is to use:

System.Text.RegularExpressions.Regex.Unescape(databaseString);

More info Here

JKennedy
  • 18,150
  • 17
  • 114
  • 198