如何在 C# 中识别字符串是否为数字?
csharpprogrammingserver side programming更新于 2025/4/19 16:07:17
假设我们的字符串是 −
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

