Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent C# from escaping my string?

Tags:

c#

escaping

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

like image 548
Giedrius Avatar asked Dec 20 '25 11:12

Giedrius


2 Answers

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

like image 148
ekholm Avatar answered Dec 22 '25 04:12

ekholm


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?

like image 39
Tim Avatar answered Dec 22 '25 04:12

Tim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!