怎么判断一个string是不是都是数字?

chnking 2002-07-16 11:52:56
如题
...全文
297 5 打赏 收藏 转发到动态 举报
写回复
用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、使用验证控件判断

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧