第一种方法:正则表达式<span style="white-space:pre"> </span>Regex numRegex = new Regex(@"^\d+$"); string id = Request.QueryString["Id"]; if (numRegex.IsMatch(id)) { // id 为纯数字。。 } else { // id 包含数字以外的字符。。 }
第二种方法:
try catch方法
例:第三种方法:try { Convert.ToInt32("123"): Console.Write("是数字"); } catch(Exception ex) { Console.Write("非数字"); }
Information.IsNumeric(txt_num.Text.Trim());
但是Information在4.5中貌似没有。
但部分书上有这么写。
等有准确的消息在来修改。
本文介绍了三种判断字符串是否仅由数字组成的实用方法:使用正则表达式进行精确匹配;利用try-catch结构尝试将字符串转换为整数;以及通过调用特定方法直接验证。这些方法适用于不同的编程场景。

1410

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



