怎么判断字符串是否为数值? 在VB有个函式IsNumberic可以用来判断字符串是否为数值, 在C#要怎么才能方便的判断呢?(不论是整数,小数,负数) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在项目中添加Microsoft Visual Basic .NET Runtime的引用,然后就可以在代码中调用Microsoft.VisualBasic.Information.IsNumeric("123");了if (Microsoft.VisualBasic.Information.IsNumeric("123")){Console.Write("true");} 方案一:Try...Catch(执行效率不高)/// <summary>/// 名称:IsNumberic/// 功能:判断输入的是否是数字/// 参数:string oText:源文本/// 返回值: bool true:是 false:否/// </summary>/// <param name="oText"></param>/// <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<ch.Length;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<inString.Length;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"); LaoDai_Net(老代.Net『学无止境』) 兄, 佩服你~~~ c# winform中如何实现热键功能? C# winform C/S结构 获取本地 IP 停止模拟循环点击按钮的sendMessage()方法 在树型控件中添加右键快捷键,怎样判断是否有选中某一个节点? 请教一个字符串生成 C# 实现如何在listBox中添加不重复的值 如何控制粘贴到freetextbox编辑器的图片的宽度呢! .net 客户端控件和服务器控件用法区别 关于winform下面的datarow的rowChanged事件问题,急,急,急,急 新手提问 大家回答 form程序里如何得到IIS某虚目录的路径? toolstrip中的button的image不对劲
Microsoft.VisualBasic.Information.IsNumeric("123");了if (Microsoft.VisualBasic.Information.IsNumeric("123"))
{
Console.Write("true");
}
/// 名称:IsNumberic
/// 功能:判断输入的是否是数字
/// 参数:string oText:源文本
/// 返回值: bool true:是 false:否
/// </summary>
/// <param name="oText"></param>
/// <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<ch.Length;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<inString.Length;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");