如何确定字符串在C#中是否为数字?
让我们说我们的字符串是-
string str = "3456";
现在,检查输入的字符串是否为数字-
str.All(c => char.IsDigit(c))
如果字符串是数字,则上面的返回true,否则返回false。
这是完整的代码-
示例
using System;
using System.Linq;
namespace Demo {
public class MyApplication {
public static void Main(string[] args) {
string str = "3456";
//检查字符串是否是数字
Console.WriteLine(str.All(c => char.IsDigit(c)));
}
}
}输出结果
True