社区
C#
帖子详情
怎么判断一个string是不是都是数字?
chnking
2002-07-16 11:52:56
如题
...全文
297
5
打赏
收藏
怎么判断一个string是不是都是数字?
如题
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
「已注销」
2002-07-16
打赏
举报
回复
写个函数
private bool IsNum(string str)
{
try
{
long num=Convert.ToInt64(str);
return true;
}
catch
{
return false;
}
}
即当str全为数值时,转换成功,则返回真,否则返回假。
snewxf
2002-07-16
打赏
举报
回复
[STAThread]
static void Main(string[] args)
{
try
{
string a=Console.ReadLine();
long b=Convert.ToInt64(a);
Console.WriteLine(b);
}
catch(Exception exce)
{
Console.WriteLine(exce.Message);
//
// TODO: 在此处添加代码以启动应用程序
//
}
}
假如你输入的不是数字的话它会抛出异常!
GOOD LUCK!!!!!!!!!!
hbxtx
2002-07-16
打赏
举报
回复
string s=TextBox1.Text;
bool flag=false;
for(int i=0;i<s.Length-1;i++){
if(Char.IsDigit(s[i]))
flag=true;
}
if(flag)
Label1.Text="是数字";
else
Label1.Text="不是数字";
hbxtx
2002-07-16
打赏
举报
回复
string s=TextBox1.Text;
bool flag=false;
for(int i=0;i<s.Length-1;i++){
if(Char.IsDigit(s[i]))
flag=true;
}
if(flag)
Label1.Text="是数字";
else
Label1.Text="不是数字";
redcaff_l
2002-07-16
打赏
举报
回复
有多种方法。
1、写一个方法:
private bool CheckNumbers(string aCheckItem, string aCheckValue)
{
for(int i = 0; i < aCheckValue.Length; i++)
{
string str = aCheckValue.Substring(i,1);
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("^[0-9]$");
System.Text.RegularExpressions.Match match = reg.Match(str);
if(!match.Success)
{
return false;
}
}
return true;
}
2、使用javascript判断。
3、使用验证控件判断
Java
判断
一个
String
是否全为
数字
组成的6中方法(转)
(转)java
判断
string
变量是否是
数字
的六种方法小结 - 77庁長 - 博客园
C#
判断
一个
string
是否为
数字
C#
判断
一个
string
是否为
数字
案一:Try...Catch(执行效率不高) private bool IsNumberic(
string
oText) { try { int var1=Convert.ToInt32 (oText); return true; ...
C++如何
判断
一个
string
字符串,是否是
数字
方法一:
判断
字符的ASCII范围(
数字
的范围为48~57) #include using namespace std; bool AllisNum(
string
str); int main( void ) {
string
str1 = "wolaiqiao23";
string
str2 = "1990"; if ...
java
判断
string
是
数字
_java
判断
string
是否为
数字
的方法
java中
判断
字符串是否为
数字
的方法:1、用JAVA自带的函数public static boolean isNumeric(
String
str){for (int i = 0; i < str.length(); i++){System.out.println(str.charAt(i));if (!Character.isDigit(str....
判断
一个
string
全由
数字
组成
1.正则
判断
const str = '123' if (/^\d+$/.test(str)) { //全为
数字
,执行... } // 或者 if (/^[0-9]+$/.test(str)) { //全为
数字
,执行... } 2.isNaN
判断
... // str是个
数字
,执行... } ...
C#
110,533
社区成员
642,574
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章