♣
怎么判断字符串是否为数值?
在vb有个函式isnumberic可以用来判断字符串是否为数值, 在c#要怎么才能方便的判断呢?(不论是整数,小数,负数)
· 网友精彩回答:
在项目中添加microsoft visual basic .net runtime的引用,然后就可以在代码中调用
microsoft.visualbasic.information.isnumeric("123");了
if (microsoft.visualbasic.information.isnumeric("123"))
{
console.write("true");
}
方案一:try...catch(执行效率不高)
/// <summary>
/// 名称:isnumberic
/// 功能:判断输入的是否是数字
/// 参数:string otext:源文本
/// 返回值: bool true:是 false:否
/// </summary>
/// <param name="otext"></param>
/// <returns></returns>
private bool isnumberic(string otext)
{
try
{
int var1=convert.toint32 (otext);
return true;
}
catch
{
return false;
}
}
方案二:正则表达式(推荐)
a)
using system;
using system.text.regularexpressions;
public bool isnumber(string strnumber)
{
regex objnotnumberpattern=new regex("[^0-9.-]");
regex objtwodotpattern=new regex("[0-9]*[.][0-9]*[.][0-9]*");
regex objtwominuspattern=new regex("[0-9]*[-][0-9]*[-][0-9]*");
string strvalidrealpattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
string strvalidintegerpattern="^([-]|[0-9])[0-9]*$";
regex objnumberpattern =new regex("(" + strvalidrealpattern +")|(" + strvalidintegerpattern + ")");
return !objnotnumberpattern.ismatch(strnumber) &&
!objtwodotpattern.ismatch(strnumber) &&
!objtwominuspattern.ismatch(strnumber) &&
objnumberpattern.ismatch(strnumber);
}
b)
public static bool isnumeric(string value)
{
return regex.ismatch(value, @"^[+-]?\d*[.]?\d*$");
}
public static bool isint(string value)
{
return regex.ismatch(value, @"^[+-]?\d*$");
}
public static bool isunsign(string value)
{
return regex.ismatch(value, @"^\d*[.]?\d*$");
}
方案三:遍历
a)
public bool isnumeric(string str)
{
char[] ch=new char[str.length];
ch=str.tochararray();
for(int i=0;i<ch.length;i++)
{
if(ch[i]<48 || ch[i]>57)
return false;
}
return true;
}
b)
public bool isinteger(string strin) {
bool bolresult=true;
if(strin=="") {
bolresult=false;
}
else {
foreach(char char in strin) {
if(char.isnumber(char))
continue;
else {
bolresult=false;
break;
}
}
}
return bolresult;
}
c)
public static bool isnumeric(string instring)
{
instring=instring.trim();
bool havenumber=false;
bool havedot=false;
for(int i=0;i<instring.length;i++)
{
if (char.isnumber(instring[i]))
{
havenumber=true;
}
else if(instring[i]==.)
{
if (havedot)
{
return false;
}
else
{
havedot=true;
}
}
else if(i==0)
{
if(instring[i]!=+&&instring[i]!=-)
{
return false;
}
}
else
{
return false;
}
if(i>20)
{
return false;
}
}
return havenumber;
}
}
方案四:改写vb的isnumeric源代码(执行效率不高)
//主调函数
public static bool isnumeric(object expression)
{
bool flag1;
iconvertible convertible1 = null;
if (expression is iconvertible)
{
convertible1 = (iconvertible) expression;
}
if (convertible1 == null)
{
if (expression is char[])
{
expression = new string((char[]) expression);
}
else
{
return false;
}
}
typecode code1 = convertible1.gettypecode();
if ((code1 != typecode.string) && (code1 != typecode.char))
{
return utils.isnumerictypecode(code1);
}
string text1 = convertible1.tostring(null);
try
{
long num2;
if (!stringtype.ishexoroctvalue(text1, ref num2))
{
double num1;
return doubletype.tryparse(text1, ref num1);
}
flag1 = true;
}
catch (exception)
{
flag1 = false;
}
return flag1;
}
//子函数
// return utils.isnumerictypecode(code1);
internal static bool isnumerictypecode(typecode typcode)
{
switch (typcode)
{
case typecode.boolean:
case typecode.byte:
case typecode.int16:
case typecode.int32:
case typecode.int64:
case typecode.single:
case typecode.double:
case typecode.decimal:
{
return true;
}
case typecode.char:
case typecode.sbyte:
case typecode.uint16:
case typecode.uint32:
case typecode.uint64:
{
break;
}
}
return false;
}
//-----------------
//stringtype.ishexoroctvalue(text1, ref num2))
internal static bool ishexoroctvalue(string value, ref long i64value)
{
int num1;
int num2 = value.length;
while (num1 < num2)
{
char ch1 = value[num1];
if (ch1 == &)
{
ch1 = char.tolower(value[num1 + 1], cultureinfo.invariantculture);
string text1 = stringtype.tohalfwidthnumbers(value.substring(num1 + 2));
if (ch1 == h)
{
i64value = convert.toint64(text1, 0x10);
}
else if (ch1 == o)
{
i64value = convert.toint64(text1, 8);
}
else
{
throw new formatexception();
}
return true;
}
if ((ch1 != ) && (ch1 != \u3000))
{
return false;
}
num1++;
}
return false;
}
//----------------------------------------------------
// doubletype.tryparse(text1, ref num1);
internal static bool tryparse(string value, ref double result)
{
bool flag1;
cultureinfo info1 = utils.getcultureinfo();
numberformatinfo info3 = info1.numberformat;
numberformatinfo info2 = decimaltype.getnormalizednumberformat(info3);
value = stringtype.tohalfwidthnumbers(value, info1);
if (info3 == info2)
{
return double.tryparse(value, numberstyles.any, info2, out result);
}
try
{
result = double.parse(value, numberstyles.any, info2);
flag1 = true;
}
catch (formatexception)
{
flag1 = double.tryparse(value, numberstyles.any, info3, out result);
}
catch (exception)
{
flag1 = false;
}
return flag1;
}
方案五: 直接引用vb运行库(执行效率不高)
方法: 首先需要添加visualbasic.runtime的引用
代码中using microsoft.visualbasic;
程序中用information.isnumeric("ddddd");
- 更多问题:
- · 如何把TC写的图形代码运行结果,屏幕图形保存在WORD文档中?
- · 两点了,我睡不着! 是数学还是习题? 想听听大家的意见
- · 如何使非模式对话框 (showModelessDialog())隐藏在主窗口背后?
- · 如何屏蔽“ctrl+v”和“右键的粘贴功能”??
- · 如何使非模式对话框 (showModelessDialog())隐藏在主窗口背后?
- · 变量取值中遇到的问题
- · 怎样把SELECT元素中的OPTION项之前加上一个复选框CHECKBOX?
- · 变量取值中遇到的问题
- · 如何编辑WebBrowser控件中的源文件?
- · 两点了,我睡不着! 是数学还是习题? 想听听大家的意见
- · 如何得到窗口外的鼠标动作
- · 两点了,我睡不着! 是数学还是习题? 想听听大家的意见
- · 两点了,我睡不着!是数学还是习题?
- · 如何在多个windows窗口中共享数据连接
- · 紧急求助,如何将保存RGB值的数组,转化成一张BMP图像并且显示,在线等待
- · 我想数据保存后不能修改,请问各位老师以下的代码错在哪里?
- · mfc编程
- · 脚本应用 | 脚本
- · 手册大全
- · jsp入门
- · 菜单编程 | 菜单
- · 系统集成开发
- · apache教程
- · apache windows
- · ie缓存
- · 漏洞扫描
- · 漏洞扫描器
- · svchost.exe 魔波
- · windows进程
- · apache支持asp
- · apache的配置
- · fso 方法
- · 在R5的管理客户端,如何用server.id来签名设计元素
- · 全球知名3D设计软件行业盛会首度移师中国
- · 形象设计
- · 三维自然景观设计大师Vue 5 Esprit
- · 程序设计
- · Photoshop7.0之系统优化策略
- · 速成高手 Photoshop中文版数码全攻略
- · photoshop字体
- · firefox浏览器

