3

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

anton.burger
  • 5,637
  • 32
  • 48
Giedrius
  • 8,430
  • 6
  • 50
  • 91
  • 2
    That's just a debugger display artifact, it is not the language doing this. A \n is only valid in literals in your code, parsed by the compiler. You cannot type it into, say, the Properties window. Or a TextBox, you'd need to set Multiline = true and press the Enter key. – Hans Passant Jun 19 '12 at 13:01
  • The debug view is displaying the string escaped, but I think you'll find that the string doesn't actually contain the escape. Output the string to a MessageBox and see if the escape is really there. – Jim Mischel Jun 19 '12 at 13:03
  • Did you try to do "{sku}:{value}" + '\n'.ToString(), had an simmilar issue, where it worked to use an "char" instead. – Rasmus Søborg Jun 19 '12 at 13:05
  • @HansPassant, Jim: If it is debugger display artifact, why after using StringBuilder.AppendFormat(textbox.Text, args) i see \n instead of new lines? – Giedrius Jun 19 '12 at 13:15
  • 1
    The debugger shows you the string the way you'd write it in your source code. Click the magnifier icon to bring up the text visualizer. It shows you how it looks on the screen. – Hans Passant Jun 19 '12 at 13:18

3 Answers3

8

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

Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
ekholm
  • 2,543
  • 18
  • 18
  • +1 yup - because when you create a string from input (say from a textbox) like `"\n"` that's the exact string that's getting created & which means nothing special to the string at runtime. Whereas the escape `\n` in a C# code file is an instruction to the compiler to emit a newline (character code 13 in decimal). – Andras Zoltan Jun 19 '12 at 13:15
  • Would be nice to have generic approach, to handle \t and etc, but if there will be none, will accept as an answer. – Giedrius Jun 19 '12 at 13:16
  • 2
    @Giedrus you can't really (unless you do something nasty like invoke the C# compiler to compile a string literal from the input and then grab it back) because you're effectively creating your own language and therefore have to parse it and translate it. The escape sequences we know and love are C#-related, not .Net related. – Andras Zoltan Jun 19 '12 at 13:18
  • 2
    Well, there aren't that many of them, see [this link](http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cf2e3220-dc8d-4de7-96d3-44dd93a52423), so it's an easy task checking all of them in a method – ekholm Jun 19 '12 at 13:24
  • 2
    I totally agree; and yes a simple solution is more than achievable - I was just responding to what appeared to me to be a belief from @Giedrius that escape sequences are somehow embedded in the framework, which they're not. A simple map of symbols and replacements will work perfectly here. – Andras Zoltan Jun 19 '12 at 13:30
4

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?

Community
  • 1
  • 1
Tim
  • 3,755
  • 3
  • 36
  • 57
0

Thanks very much, this is what a was search for