C#_C#判断一个String是否为数字类型,方案一:Try...Catch(执行效率不
}
if ((code1 != TypeCode.String) && (code1 != TypeCode.Char))
复制代码 代码如下:
{}
else
} bolResult=false;
}
case TypeCode.Char:
} }
flag1 = true;
try
return double.TryParse(Value, NumberStyles.Any, info2, out Result);
{
return Utils.IsNumericTypeCode(code1);
ch1 = char.ToLower(Value[num1 + 1], CultureInfo.InvariantCulture);
Regex objNotNumberPattern=new Regex("[^0-9.-]");
{
方法:首先需要添加Visualbasic.runtime的引用
case TypeCode.Boolean:
a)
代码中Using Microsoft.visualbasic;case TypeCode.Int64:
{
bool haveDot = false;
{
i64Value = Convert.ToInt64(text1, 8);
复制代码 代码如下:
}!objTwoDotPattern.IsMatch(strNumber) &&
return !objNotNumberPattern.IsMatch(strNumber) &&
case TypeCode.Decimal:
if ((ch1 != ' ') && (ch1 != '/u3000'))
for (int i = 0; i < inString.Length; i++)
// return Utils.IsNumericTypeCode(code1);
CultureInfo info1 = Utils.GetCultureInfo();
case TypeCode.UInt64:
Regex objNumberPattern =new Regex("(" + strValidRealPattern +")|(" + strValidIntegerPattern + ")");
}
} foreach(char Char in strIn) {
程序中用Information.isnumeric("ddddd"); {
return Regex.IsMatch(value, @"^/d*[.]?/d*$");
//StringType.IsHexOrOctValue(text1, ref num2))
int num2 = Value.Length;
b)
//----------------------------------------------------String strValidIntegerPattern="^([-]|[0-9])[0-9]*$";
char ch1 = Value[num1];
}
if (inString[i] != '+' && inString[i] != '-')
}
else if (inString[i] == '.')
long num2;
}
Result = double.Parse(Value, NumberStyles.Any, info2);
{
{
{
{
}
}
{
return DoubleType.TryParse(text1, ref num1);
}
return haveNumber;
}
{
public static bool IsNumeric(object Expression)
private bool IsNumberic(string oText)
break;
复制代码 代码如下:
for(int i=0;i {}
switch (TypCode)
try
if(ch[i]<48 || ch[i]>57)
}
} continue;
int num1;
if(strIn=="") {
{
flag1 = double.TryParse(Value, NumberStyles.Any, info3, out Result);
}
case TypeCode.Int16:
}
internal static bool TryParse(string Value, ref double Result)
if (Expression is char[])
public static bool IsUnsign(string value)
}
方案一:Try...Catch(执行效率不高)
while (num1 < num2)}
convertible1 = (IConvertible) Expression;
}
return false;
{
public bool IsNumber(String strNumber)
{
}
bool flag1;
}
{
i64Value = Convert.ToInt64(text1, 0x10);
}
Regex objTwoMinusPattern=new Regex("[0-9]*[-][0-9]*[-][0-9]*");
flag1 = true;
return Regex.IsMatch(value, @"^[+-]?/d*[.]?/d*$");
}
方案四:改写vb的IsNumeric源代码(执行效率不高)
flag1 = false;{
case TypeCode.Single:
}
return false;
}
return bolResult;
{
} {
{
}
if (ch1 == 'h')
internal static bool IsHexOrOctValue(string Value, ref long i64Value)
} else
internal static bool IsNumericTypeCode(TypeCode TypCode)
inString = inString.Trim();
objNumberPattern.IsMatch(strNumber);
{
return false;
public static bool IsNumeric(string value)
catch (FormatException)
return true;
else
return false;
if (i > 20)
{
{
{
return true;
return false;
return false;
else if (i == 0)
haveNumber = true;
return flag1;
{
else {
bool bolResult=true;
ch=str.ToCharArray();
else {
string text1 = StringType.ToHalfwidthNumbers(Value.Substring(num1 + 2));
复制代码 代码如下:
{}
{
复制代码 代码如下:
case TypeCode.Byte:方案五:直接引用vb运行库(执行效率不高)
case TypeCode.Double:a)
{}
{
{
if (Expression is IConvertible)
using System;
using System.Text.RegularExpressions;
if (Char.IsNumber(inString[i]))
NumberFormatInfo info2 = DecimalType.GetNormalizedNumberFormat(info3);
case TypeCode.UInt16:
if(char.IsNumber(Char))
num1++;
public bool IsInteger(string strIn) {
}
case TypeCode.UInt32:
break;
{
char[] ch=new char[str.Length];
}
haveDot = true;
String strValidRealPattern="^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
return false;
public static bool isNumeric(string inString)
case TypeCode.SByte:
public bool isnumeric(string str)
return true;
return true;
}
}
复制代码 代码如下:
bool flag1;TypeCode code1 = convertible1.GetTypeCode();
if (convertible1 == null)
}
Value = StringType.ToHalfwidthNumbers(Value, info1);
return false;
{
}
return false;
{
{
方案三:遍历
c)
if (info3 == info2)Regex objTwoDotPattern=new Regex("[0-9]*[.][0-9]*[.][0-9]*");
if (!StringType.IsHexOrOctValue(text1, ref num2))
{
}
复制代码 代码如下:
catch (Exception)return Regex.IsMatch(value, @"^[+-]?/d*$");
}
int var1=Convert.ToInt32 (oText);
try
//主调函数
string text1 = convertible1.ToString(null);
else if (ch1 == 'o')
catch
Expression = new string((char[]) Expression);
{
{
// DoubleType.TryParse(text1, ref num1);
case TypeCode.Int32:
}
{
throw new FormatException();
}
else
{
}
}
}//子函数
bolResult=false;
{
double num1;
return flag1;
以上就是C#判断一个String是否为数字类型的全部内容,推荐大家使用正则表达式的方法,比较简单且效率高。
if (ch1 == '&')public static bool IsInt(string value)
{
b)
flag1 = false;}
if (haveDot)
//-----------------
{
方案二:正则表达式(推荐)
bool haveNumber = false;!objTwoMinusPattern.IsMatch(strNumber) &&
相关热词: C#
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/c/5871.shtml
