用c#怎么判断输入的是数字还是字符串

疯颠的石头 2012-06-02 03:15:51
比如
string n = Console.ReadLine();
if(?)
{
//执行数字的程序
}
else
{
//执行字符串的程序
}
...全文
589 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
引用 3 楼 的回复:
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}
tring n = Console.ReadLine();
int num;
if (IsNumeric(n))
{
//执行数字的程序
}
else
{
……
[/Quote]

正则
__天涯寻梦 2012-06-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
引用 3 楼 的回复:
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}
tring n = Console.ReadLine();
int num;
if (IsNumeric(n))
{
//执行数字的程序
}
else
{
……
[/Quote]
1L 的方式不能满足你的要求么?
疯颠的石头 2012-06-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}
tring n = Console.ReadLine();
int num;
if (IsNumeric(n))
{
//执行数字的程序
}
else
{
//执行字符……
[/Quote]
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}
这是什么意思?
ParanoidKing 2012-06-02
  • 打赏
  • 举报
回复
[code=C#]
bool isNum = false;
double num = 0;
try
{
num = double.Parse(n);
isNum = true;
}
catch
{

}
if(isNum)
{
// do somthing with num
}
else
{
// do something with n
}
[/Code]
蝶恋花雨 2012-06-02
  • 打赏
  • 举报
回复
public static bool RegexValidate(string validateString)
{
Regex regex = new Regex("^[0-9]*$");
return regex.IsMatch(validateString.Trim());
}
string n = Console.ReadLine();
bool isNum=RegexValidate(s);
if(bool ){}else{]
蝶恋花雨 2012-06-02
  • 打赏
  • 举报
回复
public static bool IsNumeric(string value)
{
return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
}
tring n = Console.ReadLine();
int num;
if (IsNumeric(n))
{
//执行数字的程序
}
else
{
//执行字符串的程序
}
cnfixit 2012-06-02
  • 打赏
  • 举报
回复
Char.IsNumber()
Char.IsLetter()
__天涯寻梦 2012-06-02
  • 打赏
  • 举报
回复
string n = Console.ReadLine();
int num;
if (int.TryParse(n, out num))
{
// num 就是结果数字
//执行数字的程序
}
else
{
//执行字符串的程序
}

110,566

社区成员

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

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

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