判断一个string是否可以为数字
方案一:Try...Catch(执行效率不高)
///
1<summary>
2/// 名称:IsNumberic
3/// 功能:判断输入的是否是数字
4/// 参数:string oText:源文本
5/// 返回值: bool true:是 false:否
6/// </summary>
///
1<param name="oText"/>
///
1<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
1<ch.length;i++) ch[i]="" if(ch[i]<48="" {="" ||="">57)
2return false;
3}
4return true;
5}
6
7b)
8public bool IsInteger(string strIn) {
9bool bolResult=true;
10if(strIn=="") {
11bolResult=false;
12}
13else {
14foreach(char Char in strIn) {
15if(char.IsNumber(Char))
16continue;
17else {
18bolResult=false;
19break;
20}
21}
22}
23return bolResult;
24}
25
26c)
27public static bool isNumeric(string inString)
28{
29inString=inString.Trim();
30bool haveNumber=false;
31bool haveDot=false;
32for(int i=0;i<instring.length;i++) &&instring[i]!="-" (char.isnumber(instring[i]))="" (havedot)="" )="" else="" false;="" havedot="true;" havenumber="true;" if="" if(i="" if(instring[i]="." if(instring[i]!="+" return="" {="" }="">20)
33{
34return false;
35}
36}
37return haveNumber;
38}
39}
40
41方案四:改写vb的IsNumeric源代码(执行效率不高)
42
43//主调函数
44public static bool IsNumeric(object Expression)
45{
46bool flag1;
47IConvertible convertible1 = null;
48if (Expression is IConvertible)
49{
50convertible1 = (IConvertible) Expression;
51}
52if (convertible1 == null)
53{
54if (Expression is char[])
55{
56Expression = new string((char[]) Expression);
57}
58else
59{
60return false;
61}
62}
63TypeCode code1 = convertible1.GetTypeCode();
64if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
65{
66return Utils.IsNumericTypeCode(code1);
67}
68string text1 = convertible1.ToString(null);
69try
70{
71long num2;
72if (!StringType.IsHexOrOctValue(text1, ref num2))
73{
74double num1;
75return DoubleType.TryParse(text1, ref num1);
76}
77flag1 = true;
78}
79catch (Exception)
80{
81flag1 = false;
82}
83return flag1;
84}
85
86//子函数
87// return Utils.IsNumericTypeCode(code1);
88internal static bool IsNumericTypeCode(TypeCode TypCode)
89{
90switch (TypCode)
91{
92case TypeCode.Boolean:
93case TypeCode.Byte:
94case TypeCode.Int16:
95case TypeCode.Int32:
96case TypeCode.Int64:
97case TypeCode.Single:
98case TypeCode.Double:
99case TypeCode.Decimal:
100{
101return true;
102}
103case TypeCode.Char:
104case TypeCode.SByte:
105case TypeCode.UInt16:
106case TypeCode.UInt32:
107case TypeCode.UInt64:
108{
109break;
110}
111}
112return false;
113}
114
115
116//-----------------
117//StringType.IsHexOrOctValue(text1, ref num2))
118internal static bool IsHexOrOctValue(string Value, ref long i64Value)
119{
120int num1;
121int num2 = Value.Length;
122while (num1 < num2)
123{
124char ch1 = Value[num1];
125if (ch1 == '&')
126{
127ch1 = char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);
128string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));
129if (ch1 == 'h')
130{
131i64Value = Convert.ToInt64(text1, 0x10);
132}
133else if (ch1 == 'o')
134{
135i64Value = Convert.ToInt64(text1, 8);
136}
137else
138{
139throw new FormatException();
140}
141return true;
142}
143if ((ch1 != ' ') && (ch1 != '\u3000'))
144{
145return false;
146}
147num1++;
148}
149return false;
150}
151//----------------------------------------------------
152// DoubleType.TryParse(text1, ref num1);
153internal static bool TryParse(string Value, ref double Result)
154{
155bool flag1;
156CultureInfo info1 = Utils.GetCultureInfo();
157NumberFormatInfo info3 = info1.NumberFormat;
158NumberFormatInfo info2 = DecimalType.GetNormalizedNumberFormat(info3);
159Value = StringType.ToHalfwidthNumbers(Value, info1);
160if (info3 == info2)
161{
162return double.TryParse(Value, NumberStyles.Any, info2, out Result);
163}
164try
165{
166Result = double.Parse(Value, NumberStyles.Any, info2);
167flag1 = true;
168}
169catch (FormatException)
170{
171flag1 = double.TryParse(Value, NumberStyles.Any, info3, out Result);
172}
173catch (Exception)
174{
175flag1 = false;
176}
177return flag1;
178}
179
180方案五: 直接引用vb运行库(执行效率不高)
181
182方法: 首先需要添加Visualbasic.runtime的引用
183代码中Using Microsoft.visualbasic;
184程序中用Information.isnumeric("ddddd");</instring.length;i++)></ch.length;i++)>