I need a TextBox on a WPF control that can take in text like Commit\r\n\r
(which is the .net string "Commit\\r\\n\\r"
) and convert it back to "Commit\r\n\r"
as a .net string. I was hoping for a string.Unescape() and string.Escape() method pair, but it doesn't seem to exist. Am I going to have to write my own? or is there a more simple way to do this?
Asked
Active
Viewed 3.9k times
24

Patrick McDonald
- 64,141
- 14
- 108
- 120

Firoso
- 6,647
- 10
- 45
- 91
-
2Don't type that in the text box, just press the Enter key. – Hans Passant Apr 18 '10 at 04:49
-
and if they want to say \r\n\r\r? (yes, this is a viable situation on this particular model) – Firoso Apr 18 '10 at 04:55
-
6Check this: http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/08bb20a2-82b9-4f2f-9f1a-994961fbecc3/ – Hans Passant Apr 18 '10 at 05:09
-
@Hans: good answer, please elaborate for other readers, convert to C#, and post again and I will select it as an answer. – Firoso Apr 18 '10 at 05:12
-
2@firoso: Why don't *you* do it? – Noon Silk Apr 18 '10 at 05:20
-
2@silky : I didn't provide the solution, Hans deserves the Rep. – Firoso Apr 18 '10 at 05:34
3 Answers
43
System.Text.RegularExpressions.Regex.Unescape(@"\r\n\t\t\t\t\t\t\t\t\tHello world!")

Pavel Chuchuva
- 22,633
- 10
- 99
- 115

Diego F.
- 463
- 4
- 3
-
7
-
15Beware that this is `Regex` specific and also escapes strings like `@"\["` into `@"["` which is only desired if we are talking about a regex, not a normal string. – Silvermind May 02 '14 at 11:17
10
Hans's code, improved version.
- Made it use StringBuilder - a real performance booster on long strings
Made it an extension method
public static class StringUnescape { public static string Unescape(this string txt) { if (string.IsNullOrEmpty(txt)) { return txt; } StringBuilder retval = new StringBuilder(txt.Length); for (int ix = 0; ix < txt.Length; ) { int jx = txt.IndexOf('\\', ix); if (jx < 0 || jx == txt.Length - 1) jx = txt.Length; retval.Append(txt, ix, jx - ix); if (jx >= txt.Length) break; switch (txt[jx + 1]) { case 'n': retval.Append('\n'); break; // Line feed case 'r': retval.Append('\r'); break; // Carriage return case 't': retval.Append('\t'); break; // Tab case '\\': retval.Append('\\'); break; // Don't escape default: // Unrecognized, copy as-is retval.Append('\\').Append(txt[jx + 1]); break; } ix = jx + 2; } return retval.ToString(); } }

Martin
- 10,738
- 14
- 59
- 67

Fyodor Soikin
- 78,590
- 9
- 125
- 172
-
FYI the listed function has the wrong name - it is /unescaping/ a string, but is called EscapeStringChars(). – redcalx Jun 25 '12 at 13:27
-
This seems to work better than other answers, but still doesn't support all C# escape sequences: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#unicode-character-escape-sequences – Gru Apr 12 '20 at 10:30
3
Following methods are same as javascript escape/unescape functions:
Microsoft.JScript.GlobalObject.unescape();
Microsoft.JScript.GlobalObject.escape();

Griwes
- 8,805
- 2
- 43
- 70

hs.jalilian
- 103
- 1
- 2