问题
In C# .Net I'm looking to convert an ascii value (13, 16 etc) to its escaped view (eg \n, \r, \t etc) for a selector. Is there a built in way to do this or do I have to resort to using a look up table?
回答1:
Not that I'm aware of... but it would be a very small lookup table anyway. It may be easiest to use a switch statement, in fact:
public static string Escape(char c)
{
switch (c)
{
case '\n': return "\\n";
case '\r': return "\\r";
case '\t': return "\\t";
case '\b': return "\\b";
// etc
default: return c.ToString(); // Perhaps...
}
}
You could potentially also return \uxxxx
for any nonprintable characters.
来源:https://stackoverflow.com/questions/1508523/how-do-i-convert-an-ascii-code-to-the-escaped-screen-friendly-view