public static class Extensions{public static bool IsNumeric(this string s){foreach (char c in s){if (!char.IsDigit(c) && c != '.'){return false;}}
return true;}}
//To my knowledge I did this in a simple waystatic void Main(string[] args){string a, b;int f1, f2, x, y;Console.WriteLine("Enter two inputs");a = Convert.ToString(Console.ReadLine());b = Console.ReadLine();f1 = find(a);f2 = find(b);
if (f1 == 0 && f2 == 0){x = Convert.ToInt32(a);y = Convert.ToInt32(b);Console.WriteLine("Two inputs r number \n so that addition of these text box is= " + (x + y).ToString());}elseConsole.WriteLine("One or two inputs r string \n so that concatenation of these text box is = " + (a + b));Console.ReadKey();}
static int find(string s){string s1 = "";int f;for (int i = 0; i < s.Length; i++)for (int j = 0; j <= 9; j++){string c = j.ToString();if (c[0] == s[i]){s1 += c[0];}}
if (s == s1)f = 0;elsef = 1;
return f;}
// 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 );}
public static class Extensions{/// <summary>/// Returns true if string is numeric and not empty or null or whitespace./// Determines if string is numeric by parsing as Double/// </summary>/// <param name="str"></param>/// <param name="style">Optional style - defaults to NumberStyles.Number (leading and trailing whitespace, leading and trailing sign, decimal point and thousands separator) </param>/// <param name="culture">Optional CultureInfo - defaults to InvariantCulture</param>/// <returns></returns>public static bool IsNumeric(this string str, NumberStyles style = NumberStyles.Number,CultureInfo culture = null){double num;if (culture == null) culture = CultureInfo.InvariantCulture;return Double.TryParse(str, style, culture, out num) && !String.IsNullOrWhiteSpace(str);}}
简单易用:
var mystring = "1234.56789";var test = mystring.IsNumeric();
或者,如果您想测试其他类型的数字,您可以指定“样式”。因此,要将数字转换为指数,您可以使用:
var mystring = "5.2453232E6";var test = mystring.IsNumeric(style: NumberStyles.AllowExponent);
或者要测试潜在的十六进制字符串,您可以使用:
var mystring = "0xF67AB2";var test = mystring.IsNumeric(style: NumberStyles.HexNumber)
public static class ExtensionMethods{/// <summary>/// Returns true if string could represent a valid number, including decimals and local culture symbols/// </summary>public static bool IsNumeric(this string s){decimal d;return decimal.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out d);}
/// <summary>/// Returns true only if string is wholy comprised of numerical digits/// </summary>public static bool IsNumbersOnly(this string s){if (s == null || s == string.Empty)return false;
foreach (char c in s){if (c < '0' || c > '9') // Avoid using .IsDigit or .IsNumeric as they will return true for other charactersreturn false;}
return true;}}
public static bool IsNumeric(this string input){int n;if (!string.IsNullOrEmpty(input)) //.Replace('.',null).Replace(',',null){foreach (var i in input){if (!int.TryParse(i.ToString(), out n)){return false;}
}return true;}return false;}
new Regex(@"^\d{4}").IsMatch("6") // falsenew Regex(@"^\d{4}").IsMatch("68ab") // falsenew Regex(@"^\d{4}").IsMatch("1111abcdefg")new Regex(@"^\d+").IsMatch("6") // true (any length but at least one digit)