如果我有这些字符串:
"abc" =假 "123" =真 "ab2" = false
是否有一个命令,如IsNumeric()或其他命令,可以识别字符串是否是有效的数字?
如果我有这些字符串:
"abc" =假 "123" =真 "ab2" = false
是否有一个命令,如IsNumeric()或其他命令,可以识别字符串是否是有效的数字?
当前回答
试试下面定义的正则表达式
new Regex(@"^\d{4}").IsMatch("6") // false
new Regex(@"^\d{4}").IsMatch("68ab") // false
new Regex(@"^\d{4}").IsMatch("1111abcdefg")
new Regex(@"^\d+").IsMatch("6") // true (any length but at least one digit)
其他回答
bool is_number(string str, char delimiter = '.')
{
if(str.Length==0) //Empty
{
return false;
}
bool is_delimetered = false;
foreach (char c in str)
{
if ((c < '0' || c > '9') && (c != delimiter)) //ASCII table check. Not a digit && not delimeter
{
return false;
}
if (c == delimiter)
{
if (is_delimetered) //more than 1 delimiter
{
return false;
}
else //first time delimiter
{
is_delimetered = true;
}
}
}
return true;
}
所有的答案都很有用。但是在寻找数值为12位数或更多的解决方案时(在我的情况下),然后在调试时,我发现以下解决方案很有用:
double tempInt = 0;
bool result = double.TryParse("Your_12_Digit_Or_more_StringValue", out tempInt);
结果变量会给你true或false。
我猜这个答案会被淹没在其他答案中,但不管怎样,开始吧。
我最终通过谷歌解决了这个问题,因为我想检查字符串是否为数字,这样我就可以使用double.Parse(“123”)而不是TryParse()方法。
为什么?因为在知道解析是否失败之前,必须声明一个out变量并检查TryParse()的结果是很烦人的。我想使用三元运算符来检查字符串是否为数值,然后在第一个三元表达式中解析它或在第二个三元表达式中提供默认值。
是这样的:
var doubleValue = IsNumeric(numberAsString) ? double.Parse(numberAsString) : 0;
它只是比:
var doubleValue = 0;
if (double.TryParse(numberAsString, out doubleValue)) {
//whatever you want to do with doubleValue
}
我为这些情况做了一些扩展方法:
可拓方法一
public static bool IsParseableAs<TInput>(this string value) {
var type = typeof(TInput);
var tryParseMethod = type.GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder,
new[] { typeof(string), type.MakeByRefType() }, null);
if (tryParseMethod == null) return false;
var arguments = new[] { value, Activator.CreateInstance(type) };
return (bool) tryParseMethod.Invoke(null, arguments);
}
例子:
"123".IsParseableAs<double>() ? double.Parse(sNumber) : 0;
因为IsParseableAs()尝试将字符串解析为适当的类型,而不仅仅是检查字符串是否为“numeric”,所以应该是相当安全的。您甚至可以将它用于具有TryParse()方法的非数值类型,如DateTime。
该方法使用反射,最终调用TryParse()方法两次,当然,这不是那么有效,但并不是所有事情都必须完全优化,有时方便更重要。
此方法还可以用于轻松地将数字字符串列表解析为具有默认值的double或其他类型的列表,而无需捕获任何异常:
var sNumbers = new[] {"10", "20", "30"};
var dValues = sNumbers.Select(s => s.IsParseableAs<double>() ? double.Parse(s) : 0);
可拓方法二
public static TOutput ParseAs<TOutput>(this string value, TOutput defaultValue) {
var type = typeof(TOutput);
var tryParseMethod = type.GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public, Type.DefaultBinder,
new[] { typeof(string), type.MakeByRefType() }, null);
if (tryParseMethod == null) return defaultValue;
var arguments = new object[] { value, null };
return ((bool) tryParseMethod.Invoke(null, arguments)) ? (TOutput) arguments[1] : defaultValue;
}
这个扩展方法允许您将字符串解析为具有TryParse()方法的任何类型,它还允许您指定转换失败时要返回的默认值。
这比上面的扩展方法使用三元运算符要好,因为它只进行一次转换。它仍然使用反射…
例子:
"123".ParseAs<int>(10);
"abc".ParseAs<int>(25);
"123,78".ParseAs<double>(10);
"abc".ParseAs<double>(107.4);
"2014-10-28".ParseAs<DateTime>(DateTime.MinValue);
"monday".ParseAs<DateTime>(DateTime.MinValue);
输出:
123
25
123,78
107,4
28.10.2014 00:00:00
01.01.0001 00:00:00
我已经使用了这个函数几次:
public static bool IsNumeric(object Expression)
{
double retNum;
bool isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
return isNum;
}
但你也可以用;
bool b1 = Microsoft.VisualBasic.Information.IsNumeric("1"); //true
bool b2 = Microsoft.VisualBasic.Information.IsNumeric("1aa"); // false
从基准测试IsNumeric选项
(来源:aspalliance.com)
(来源:aspalliance.com)
如果你想知道一个字符串是否是一个数字,你可以尝试解析它:
var numberString = "123";
int number;
int.TryParse(numberString , out number);
注意,TryParse返回一个bool值,您可以使用它来检查解析是否成功。