Giedrius
Giedrius

Reputation: 8550

How to prevent C# from escaping my string?

I have Windows Forms UI, that allows me to enter format for string formatting in TextBox, like:

Enter result format: {sku}:{value} \n

Problem is, that \n is getting escaped, so result format string in debug view looks like:

{sku}:{value} \\n

And that produces \n instead of line break in result.

So question is, how to prevent this, or how to remove escaping before using in StringBuilder.AppendFormat()

Update: I've decided to add image from notepad++ window, may be that will help understand what I need and why (although my use case is a bit different, I think idea is the same): enter image description here

Upvotes: 3

Views: 15008

Answers (3)

Thanks very much, this is what a was search for

Upvotes: 0

Tim
Tim

Reputation: 3803

There is a general way of doing the unescaping of the string is to use regular expression

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

How can I Unescape and Reescape strings in .net?

Upvotes: 4

ekholm
ekholm

Reputation: 2573

Probably, what you want to do is to use the string.Replace(@"\n", "\n") method. View more at http://msdn.microsoft.com/en-us/library/system.string.replace.aspx

Upvotes: 8

Related Questions