检查有完整数字的字符串或数字和字符串的组合
本文关键字:字符串 数字 组合 检查 整数 | 更新日期: 2023-09-27 18:08:40
我想检查一个字符串是数字还是字母数字。
例如:
string test = "2323212343243423333";
string test1 = "34323df23233232323e";
我想检查测试是否只有数字。如果整个字符串有number表示返回true。否则返回false。
我该怎么做?
bool allDigits = text.All(c => char.IsDigit(c));
或
bool allDigits = text.All(char.IsDigit);
除非"数字"包括十六进制数字?当然,我的答案只适用于只包含数字的字符串。
如果字符串长度不太长,您可以尝试int.TryParse(string here)
,或者您可以自己编写函数,检查字符串中的每个字符,如
if(MyString[i]-'0'<= 9 && MyString[i]-'0'>= 0)
//then it's a digit, and check other characters this way