C#:判断一个String是否为数字
方案一:Try...Catch(执行效率不高)
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 {
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 {
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");
C#:判断一个String是否为数字的更多相关文章
- 判断一个string是否以数字开头
public static void main(String[] args) { Pattern pattern =null; String content = "30. ...
- C#判断一个string是否为数字
案一:Try...Catch(执行效率不高) private bool IsNumberic(string oText) { try { int var1=Convert.ToInt32 (oText ...
- Java判断一个字符是否是数字的几种方法的代码
在工作期间,将写内容过程经常用到的一些内容段做个记录,下面内容是关于Java判断一个字符是否是数字的几种方法的内容,希望能对码农们有好处. public class Test{ public stat ...
- Delphi 判断一个字符串是否为数字
//函 数 名: IsDigit//返 回 值: boolean//日 期:2011-03-01//参 数: String//功 能: 判断一个字符串是否为数字// ...
- java 判断一个字符串中的数字:是否为数字、是否包含数字、截取数字
题外话: JavaScript中判断一个字符是否为数字,用函数:isDigit(); 一.判断一个字符串是否都为数字 package com.cmc.util; import java.util.re ...
- 在Java中用正则表达式判断一个字符串是否是数字的方法
package chengyujia; import java.util.regex.Pattern; public class NumberUtil { /** * 判断一个字符串是否是数字. * ...
- C# 判断一个string型的时间格式是否正确
在项目开发过程中,由于各种坑爹的需求,我们可能需要用户自己手动输入时间,不过这种功能一般都出现在自己家的后台里面,咳咳,言归正传.既然如此,那么这个时候我们就需要对用户手动输入的时间格式进行验证,方法 ...
- Java 如何判断一个字符是否是数字或字母
在C++中, 可以用isdigit()判断一个字符是否是数字,可以用isalpha()判断一个字符是否是字母,还有很多,都在<cctype>头文件中 而类似的方法在JAVA中,则主要是Ch ...
- Oracle sql判断一个字段是否全数字 或含有中文
update (select length(t.name), t.* -- name,length(name) from g_enterprise_info t where nvl2(translat ...
随机推荐
- C#调用HTTP接口
string url = "http://www.***.com?id=123&username=test&userpwd=*****"; WebRequest w ...
- mysql中limit用法误区
之前一直用oracle,在分页的时候用rownumber,转换到mysql上之后,用limit做分页: 在做某个业务的时候,需要先将数据排序,再分页,在给limit上参数的时候沿用了oracle的ro ...
- zabbix client安装配置执行
1 创建zabbix 用户 groupadd zabbix; useradd -g zabbix zabbix; passwd zabbix; 两次输入password 2 下载获得zabbix的包, ...
- 【UVA】1449-Dominating Patterns(AC自己主动机)
AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...
- JS(移动端)自己封装移动端一些常用方法
/** * Created by Administrator on 2016/7/14. */ /*命名空间*/ window.lcf = {}; /*监听过渡结束的方法*/ lcf.transiti ...
- javascript 中 offsetWidth,clientWidth;offsetHeight,clientHeight的区别
javascript 中 offsetWidth 是对象的可见宽度,包滚动条等边线,会随窗口的显示大小改变 clientWidth.offsetWidth.clientHeight区别IE6.0.FF ...
- Angular-UI-Router
原文 http://www.cnblogs.com/youngdze/p/4025538.html 主题 路由器JavaScript 路由 Route 为什么用 Route AJAX 请求不会留 ...
- 详解Linq to SQL
第一部分,什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并 ...
- UVa 1585 - Score
得分是目前连续O 的个数,遇到X置0 #include <cstdio> #include <iostream> #include <cstring> using ...
- linux下socket编程-UDP
下面是UDP的服务器的代码: /* server.c */ #include <stdio.h> #include <string.h> #include <netine ...