欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Char.IsDigit与Char.IsNumber的区别[转]

发布时间:2025/3/20 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Char.IsDigit与Char.IsNumber的区别[转] 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

需要判断Char是否为数字,查看了下MSDN,发现有三种方法:

Char.IsDigit (aChar)              指示指定字符串中位于指定位置处的字符是否属于十进制数字类别

Char.IsNumber(aChar)        指示指定字符串中位于指定位置的字符是否属于数字类别

aChar>='0'&&aChar<='9'     判断aChar是否位于‘0’到‘9’之前  等同于第一种

 

用.NET Reflector 查看其实现代码:

public static bool IsNumber(char c)  
  • {  
  •     if (!IsLatin1(c))  
  •     {  
  •         return CheckNumber(CharUnicodeInfo.GetUnicodeCategory(c));  
  •     }  
  •     if (!IsAscii(c))  
  •     {  
  •         return CheckNumber(GetLatin1UnicodeCategory(c));  
  •     }  
  •     return ((c >= '0') && (c <= '9'));  
  • }   
  • public static bool IsNumber(char c) { if (!IsLatin1(c)) { return CheckNumber(CharUnicodeInfo.GetUnicodeCategory(c)); } if (!IsAscii(c)) { return CheckNumber(GetLatin1UnicodeCategory(c)); } return ((c >= '0') && (c <= '9')); }

    public static bool IsDigit(char c)  
  • {  
  •     if (!IsLatin1(c))  
  •     {  
  •         return (CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber);  
  •     }  
  •     return ((c >= '0') && (c <= '9'));  
  • }  
  • public static bool IsDigit(char c) { if (!IsLatin1(c)) { return (CharUnicodeInfo.GetUnicodeCategory(c) == UnicodeCategory.DecimalDigitNumber); } return ((c >= '0') && (c <= '9')); }

     

    Char.IsNumber 多了一步检查ASCII码。。。

    转载于:https://www.cnblogs.com/junbird-nest/archive/2012/05/29/2523664.html

    总结

    以上是生活随笔为你收集整理的Char.IsDigit与Char.IsNumber的区别[转]的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。