返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

C#判断一个string是否可以为数字的几种解决方案

时间:2012-01-29 22:41来源:知行网www.zhixing123.cn 编辑:麦田守望者

方案一:Try...Catch(执行效率不高)
view plaincopy to clipboardprint?
01./// <summary>
02./// 名称:IsNumberic
03./// 功能:判断输入的是否是数字
04./// 参数:string oText:源文本
05./// 返回值: bool true:是 false:否
06./// </summary>
07./// <param name="oText"></param>
08./// <returns></returns>
09.private bool IsNumberic(string oText)
10.{
11.try
12. {
13.int var1=Convert.ToInt32 (oText);
14.return true;
15. }
16.catch
17.{
18.return false;
19.}
20.}
方案二:正则表达式(推荐)
a)


view plaincopy to clipboardprint?
01.using System;
02.using System.Text.RegularExpressions;
03.
04.public bool IsNumber(String strNumber)
05.{
06.Regex objNotNumberPattern=new Regex("[^0-9.-]");
07.Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
08.Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
09.String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
10.String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
11.Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
12.
13.return !objNotNumberPattern.IsMatch(strNumber) &&
14.!objTwoDotPattern.IsMatch(strNumber) &&
15.!objTwoMinusPattern.IsMatch(strNumber) &&
16.objNumberPattern.IsMatch(strNumber);
17.}
b)


view plaincopy to clipboardprint?
01.public static bool IsNumeric(string value)
02.{
03.return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
04.}
05.public static bool IsInt(string value)
06.{
07.return Regex.IsMatch(value, @"^[+-]?\d*$");
08.}
09.public static bool IsUnsign(string value)
10.{
11.return Regex.IsMatch(value, @"^\d*[.]?\d*$");
12.}
方案三:遍历
a)


view plaincopy to clipboardprint?
01.public bool isnumeric(string str)
02.{
03. char[] ch=new char[str.Length];
04. ch=str.ToCharArray();
05. for(int i=0;i<ch.Length;i++)
06. {
07. if(ch[i]<48 || ch[i]>57)
08. return false;
09. }
10. return true;
11.}
b)


view plaincopy to clipboardprint?
01.public bool IsInteger(string strIn) {
02.bool bolResult=true;
03.if(strIn=="") {
04.bolResult=false;
05.}
06.else {
07.foreach(char Char in strIn) {
08.if(char.IsNumber(Char))
09.continue;
10.else {
11.bolResult=false;
12.break;
13.}
14.}
15.}
16.return bolResult;
17.}
c)


view plaincopy to clipboardprint?
01.public static bool isNumeric(string inString)
02.{
03.inString=inString.Trim();
04.bool haveNumber=false;
05.bool haveDot=false;
06.for(int i=0;i<inString.Length;i++)
07.{
08.if (Char.IsNumber(inString[i]))
09.{
10.haveNumber=true;
11.}
12.else if(inString[i]=='.')
13.{
14.if (haveDot)
15.{
16.return false;
17.}
18.else
19.{
20.haveDot=true;
21.}
22.}
23.else if(i==0)
24.{
25.if(inString[i]!='+'&&inString[i]!='-')
26.{
27.return false;
28.}
29.}
30.else
31.{
32.return false;
33.}
34.if(i>20)
35.{
36.return false;
37.}
38.}
39.return haveNumber;
40.}
41.}
方案四:改写vb的IsNumeric源代码(执行效率不高)


view plaincopy to clipboardprint?
01.//主调函数
02.public static bool IsNumeric(object Expression)
03.{
04. bool flag1;
05. IConvertible convertible1 = null;
06. if (Expression is IConvertible)
07. {
08. convertible1 = (IConvertible) Expression;
09. }
10. if (convertible1 == null)
11. {
12. if (Expression is char[])
13. {
14. Expression = new string((char[]) Expression);
15. }
16. else
17. {
18. return false;
19. }
20. }
21. TypeCode code1 = convertible1.GetTypeCode();
22. if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
23. {
24. return Utils.IsNumericTypeCode(code1);
25. }
26. string text1 = convertible1.ToString(null);
27. try
28. {
29. long num2;
30. if (!StringType.IsHexOrOctValue(text1, ref num2))
31. {
32. double num1;
33. return DoubleType.TryParse(text1, ref num1);
34. }
35. flag1 = true;
36. }
37. catch (Exception)
38. {
39. flag1 = false;
40. }
41. return flag1;
42.}
43.
44.//子函数
45.// return Utils.IsNumericTypeCode(code1);
46.internal static bool IsNumericTypeCode(TypeCode TypCode)
47.{
48. switch (TypCode)
49. {
50. case TypeCode.Boolean:
51. case TypeCode.Byte:
52. case TypeCode.Int16:
53. case TypeCode.Int32:
54. case TypeCode.Int64:
55. case TypeCode.Single:
56. case TypeCode.Double:
57. case TypeCode.Decimal:
58. {
59. return true;
60. }
61. case TypeCode.Char:
62. case TypeCode.SByte:
63. case TypeCode.UInt16:
64. case TypeCode.UInt32:
65. case TypeCode.UInt64:
66. {
67. break;
68. }
69. }
70. return false;
71.}
72.
73.
74.//-----------------
75.//StringType.IsHexOrOctValue(text1, ref num2))
76.internal static bool IsHexOrOctValue(string Value, ref long i64Value)
77.{
78. int num1;
79. int num2 = Value.Length;
80. while (num1 < num2)
81. {
82. char ch1 = Value[num1];
83. if (ch1 == '&')
84. {
85. ch1 = char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);
86. string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));
87. if (ch1 == 'h')
88. {
89. i64Value = Convert.ToInt64(text1, 0x10);
90. }
91. else if (ch1 == 'o')
92. {
93. i64Value = Convert.ToInt64(text1, 8);
94. }
95. else
96. {
97. throw new FormatException();
98. }
99. return true;
100. }
101. if ((ch1 != ' ') && (ch1 != '\u3000'))
102. {
103. return false;
104. }
105. num1++;
106. }
107. return false;
108.}
109.//----------------------------------------------------
110.// DoubleType.TryParse(text1, ref num1);
111.internal static bool TryParse(string Value, ref double Result)
112.{
113. bool flag1;
114. CultureInfo info1 = Utils.GetCultureInfo();
115. NumberFormatInfo info3 = info1.NumberFormat;
116. NumberFormatInfo info2 = DecimalType.GetNormalizedNumberFormat(info3);
117. Value = StringType.ToHalfwidthNumbers(Value, info1);
118. if (info3 == info2)
119. {
120. return double.TryParse(Value, NumberStyles.Any, info2, out Result);
121. }
122. try
123. {
124. Result = double.Parse(Value, NumberStyles.Any, info2);
125. flag1 = true;
126. }
127. catch (FormatException)
128. {
129. flag1 = double.TryParse(Value, NumberStyles.Any, info3, out Result);
130. }

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):C# C#实例教程 c#基础教程 C#源代码 c#技巧
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片