How do I convert an ascii code to the escaped 'screen friendly' view?

丶灬走出姿态 提交于 2020-01-17 08:21:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!