Fre*_*örk 168
"abc3def".Any(c => char.IsDigit(c));
Run Code Online (Sandbox Code Playgroud)
更新:正如@Cipher指出的那样,它实际上可以更短:
"abc3def".Any(char.IsDigit);
Run Code Online (Sandbox Code Playgroud)
Jar*_*Par 15
试试这个
public static bool HasNumber(this string input) {
return input.Where(x => Char.IsDigit(x)).Any();
}
Run Code Online (Sandbox Code Playgroud)
用法
string x = GetTheString();
if ( x.HasNumber() ) {
...
}
Run Code Online (Sandbox Code Playgroud)
小智 8
或可能使用正则表达式:
string input = "123 find if this has a number";
bool containsNum = Regex.IsMatch(input, @"\d");
if (containsNum)
{
//Do Something
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63147 次 |
| 最近记录: |