In C#, how can I detect if a character is a non-ASCII character?

故事扮演 提交于 2020-01-01 08:23:13

问题


I would like to check, in C#, if a char contains a non-ASCII character. What is the best way to check for special characters such as or Ω?


回答1:


ASCII ranges from 0 - 127, so just check for that range:

char c = 'a';//or whatever char you have
bool isAscii = c < 128;



回答2:


    bool HasNonASCIIChars(string str)
    {
        return (System.Text.Encoding.UTF8.GetByteCount(str) != str.Length);
    }


来源:https://stackoverflow.com/questions/18596245/in-c-how-can-i-detect-if-a-character-is-a-non-ascii-character

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