Get string with escape character

Hi.

I have a textbox where I set this text, for example:

This is the sample text\n

Then I assign the text to a string variable:

string var = textbox1.Text;

So, in var I have the string"This is the sample text\\n". I would like to parse the"\\n" to"\n"... How can I do that?

Thanks.

[498 byte] By [Matt] at [2008-1-4]
# 1
Nothing in the framework to help with that. Tricky code, hope I got it right:

public static string EscapeStringChars(string txt) {
if (string.IsNullOrEmpty(txt)) return txt;
string retval = "";
for (int ix = 0; ix < txt.Length; ) {
int jx = txt.IndexOf('\\', ix);
if (jx < 0 || jx == txt.Length - 1) jx = txt.Length;
retval += txt.Substring(ix, jx - ix);
if (jx >= txt.Length) break;
switch (txt[jx + 1]) {
case 'n': retval += '\n'; break; // Line feed
case 'r': retval += '\r'; break; // Carriage return
case 't': retval += '\t'; break; // Tab
case '\\': retval += '\\'; break; // Don't escape
default: // Unrecognized, copy as-is
retval += '\\'; retval += txt[jx + 1]; break;
}
ix = jx + 2;
}
return retval;
}

nobugz at 2007-9-25 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified