通过正则表达式判定判断字符串是否为全数字
/// <summary>
/// 判断字符串是否是数字
/// </summary>
public static bool IsNumber(string s)
{
if (string.IsNullOrWhiteSpace(s)) return false;
const string pattern = "^[0-9]*$";
Regex rx = new Regex(pattern);
return rx.IsMatch(s);
}
通过字符的编码
该代码段定义了一个名为IsNumber的公共静态方法,用于判断输入的字符串s是否只包含数字。它首先检查字符串是否为空或仅含空白,然后定义一个正则表达式模式^[0-9]*$,并用此模式创建一个Regex对象。如果字符串匹配这个模式,方法返回true,表示字符串是全数字;否则返回false。
8210

被折叠的 条评论
为什么被折叠?



