如果我有这些字符串:
"abc" =假 "123" =真 "ab2" = false
是否有一个命令,如IsNumeric()或其他命令,可以识别字符串是否是有效的数字?
如果我有这些字符串:
"abc" =假 "123" =真 "ab2" = false
是否有一个命令,如IsNumeric()或其他命令,可以识别字符串是否是有效的数字?
当前回答
最好的灵活解决方案是.net内置函数- char.IsDigit。它适用于无限长的数字。只有当每个字符都是数字时,它才会返回true。我使用它很多次,没有任何问题,而且我找到了更简单的解决方案。我做了一个示例方法。它可以使用了。此外,我还增加了对空输入的验证。所以这个方法现在是完全无懈可击的
public static bool IsNumeric(string strNumber)
{
if (string.IsNullOrEmpty(strNumber))
{
return false;
}
else
{
int numberOfChar = strNumber.Count();
if (numberOfChar > 0)
{
bool r = strNumber.All(char.IsDigit);
return r;
}
else
{
return false;
}
}
}
其他回答
您可以使用TryParse来确定该字符串是否可以解析为整数。
int i;
bool bNum = int.TryParse(str, out i);
布尔值会告诉你它是否有效。
对于许多数据类型,您总是可以使用内置的TryParse方法来查看所讨论的字符串是否会通过。
的例子。
decimal myDec;
var Result = decimal.TryParse("123", out myDec);
结果将= True
decimal myDec;
var Result = decimal.TryParse("abc", out myDec);
结果将= False
最好的灵活解决方案是.net内置函数- char.IsDigit。它适用于无限长的数字。只有当每个字符都是数字时,它才会返回true。我使用它很多次,没有任何问题,而且我找到了更简单的解决方案。我做了一个示例方法。它可以使用了。此外,我还增加了对空输入的验证。所以这个方法现在是完全无懈可击的
public static bool IsNumeric(string strNumber)
{
if (string.IsNullOrEmpty(strNumber))
{
return false;
}
else
{
int numberOfChar = strNumber.Count();
if (numberOfChar > 0)
{
bool r = strNumber.All(char.IsDigit);
return r;
}
else
{
return false;
}
}
}
如果你想捕获更广泛的数字,à la PHP的is_numeric,你可以使用以下方法:
// From PHP documentation for is_numeric
// (http://php.net/manual/en/function.is-numeric.php)
// Finds whether the given variable is numeric.
// Numeric strings consist of optional sign, any number of digits, optional decimal part and optional
// exponential part. Thus +0123.45e6 is a valid numeric value.
// Hexadecimal (e.g. 0xf4c3b00c), Binary (e.g. 0b10100111001), Octal (e.g. 0777) notation is allowed too but
// only without sign, decimal and exponential part.
static readonly Regex _isNumericRegex =
new Regex( "^(" +
/*Hex*/ @"0x[0-9a-f]+" + "|" +
/*Bin*/ @"0b[01]+" + "|" +
/*Oct*/ @"0[0-7]*" + "|" +
/*Dec*/ @"((?!0)|[-+]|(?=0+\.))(\d*\.)?\d+(e\d+)?" +
")$" );
static bool IsNumeric( string value )
{
return _isNumericRegex.IsMatch( value );
}
单元测试:
static void IsNumericTest()
{
string[] l_unitTests = new string[] {
"123", /* TRUE */
"abc", /* FALSE */
"12.3", /* TRUE */
"+12.3", /* TRUE */
"-12.3", /* TRUE */
"1.23e2", /* TRUE */
"-1e23", /* TRUE */
"1.2ef", /* FALSE */
"0x0", /* TRUE */
"0xfff", /* TRUE */
"0xf1f", /* TRUE */
"0xf1g", /* FALSE */
"0123", /* TRUE */
"0999", /* FALSE (not octal) */
"+0999", /* TRUE (forced decimal) */
"0b0101", /* TRUE */
"0b0102" /* FALSE */
};
foreach ( string l_unitTest in l_unitTests )
Console.WriteLine( l_unitTest + " => " + IsNumeric( l_unitTest ).ToString() );
Console.ReadKey( true );
}
请记住,仅仅因为值是数字类型并不意味着它可以转换为数字类型。例如,“999999999999999999999999999999999999999999999999”是一个完全有效的数值,但它不适合. net数值类型(也就是说,不是标准库中定义的数值类型)。
你还可以使用:
using System.Linq;
stringTest.All(char.IsDigit);
它将返回true对于所有数字(不是浮点)和false如果输入字符串是任何类型的字母数字。
Test case | Return value | Test result |
---|---|---|
"1234" |
true | ✅Pass |
"1" |
true | ✅Pass |
"0" |
true | ✅Pass |
"" |
true | ⚠️Fail (known edge case) |
"12.34" |
false | ✅Pass |
"+1234" |
false | ✅Pass |
"-13" |
false | ✅Pass |
"3E14" |
false | ✅Pass |
"0x10" |
false | ✅Pass |
请注意:stringTest不应该是空字符串,因为这将通过是否是数字的测试。