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 ...
随机推荐
- Longtail Hedgehog(DP)
Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- springMVC3学习(四)--訪问静态文件如js,jpg,css
假设你的DispatcherServlet拦截的是*.do这种URL.就不存在訪问不到静态资源的问题 假设你的DispatcherServlet拦截了"/"全部的请求,那同一时候对 ...
- UVA 10131 Is Bigger Smarter?(DP)
Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to t ...
- python Debug 单步调试
一直犯愁的是python的调试,曾经写c都是编译完了用gdb直接调试了,轻松愉快.如今遇到这么一个解释型的程序.不知道怎么办了.用log吧,有时就是一个小程序,不想写这么多代码.打屏吧.有时屏幕翻得快 ...
- 【转载】【转自AekdyCoin的组合数取模】
本篇文章主要介绍了"[组合数求模] 转自AekdyCoin",主要涉及到[组合数求模] 转自AekdyCoin方面的内容,对于[组合数求模] 转自AekdyCoin感兴趣的同学可以 ...
- The executable was signed with invalid entitlements新设备run出现这个问题
出现这个问题一般是新手不熟悉开发者发布流程造成地 一定要安开发者流程一步一步走 这样就不会出错了 注意这几个地方地设置 1.
- 文件搜索查找功能VC++
1.搜索指定文件夹下的文件名和路径 #undef UNICODE #include <iostream> #include <string> #include <vect ...
- MySQL Workbench是一款专为MySQL设计的ER/数据库建模工具
MySQL Workbench是一款专为MySQL设计的ER/数据库建模工具.它是著名的数据库设计工具DBDesigner4的继任者.你可以用MySQL Workbench设计和创建新的数据库 ...
- hdu 2509 Be the Winner 博弈
题目链接 有n堆苹果, 对于其中的每一堆的x个苹果, 它是放在一条线上的. 你每次可以对一堆苹果进行操作, 可以取y个, 1<=y<=x. 然后如果你是取的一条线上中间的苹果, 那么这一堆 ...
- 了解神奇的this
this的用法 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不了. 因为this的取值是执行上下文环境的一部分,每次调用函数,都会产生一个新的执行上下环境.举例说 ...