【问题标题】:check the string having full number or the combinbation of number and string [duplicate]检查具有完整数字的字符串或数字和字符串的组合[重复]
【发布时间】:2013-04-03 07:57:30
【问题描述】:

我想检查一个字符串是只有数字还是字母数字。

例如:

string test = "2323212343243423333";
string test1 = "34323df23233232323e";

我想检查是否只有数字的测试。如果整个字符串有数字意味着它返回true。否则返回 false。

我该怎么做?

【问题讨论】:

标签: c# asp.net


【解决方案1】:

如果字符串长度不太长,你可以试试int.TryParse(string here),或者你可以自己写函数,检查字符串中的每个字符,比如

if(MyString[i]-'0'<= 9 && MyString[i]-'0'>= 0)
//then it's a digit, and check other characters this way

【讨论】:

  • OP给出的例子对于int.TryParse()来说肯定太长了
  • 是的,我已经说得很清楚了:)
【解决方案2】:
bool allDigits = text.All(c => char.IsDigit(c));

或者

bool allDigits = text.All(char.IsDigit);

除非“数字”包括十六进制数字?当然,我的答案只适用于只包含数字的字符串。

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 2015-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多