Determine if a string can be a number
Solution 1: Try ... catch (not high execution efficiency) ///
Solution 2: Regular expression (recommended) a) use system; use 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 string string stringRealpattern =" ^ [-] | [] | [0-9]) [0-9] * [.] * [0-9] $ "; String StrigIdintegerPattern =" ^ ([[-] | [0 -9]) [0-9] * $ "; Regex ObjNumberPattern = New Regex (" (" StrvalIDRealPattern ") ")") ")
Return! ObjNotNumberPattern.ismatch (Strnumber) &&! objtwodotpattern.ismatch (strnumber) &&! objtwominuspattern.ismatch (strnumber) && objNumberPattern.ismatch (Strnumber);}
b) public static bool isnumeric (string value) {returnes ^ [ -]? / 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 * $ ");} Solution 3: Traversing a) Public Bool IsNumeric (String Str) {char [] ch = new char [str.length]; ch = str.tochararray (); for (int i = 0; i
c) Public static bool isnumeric (String Instring) {Instring = Instring.trim (); BOOL HAVENUMBER = FALSE; BOOL HAVEDOT = FALSE; For (int i = 0; i
Scenario 4: Remote VB's ISNUMERIC source code (not high execution efficiency)
Calling function // 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;} else}} type {returse;}} 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;}
Functions // // 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 } 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;} Scenario 5: Direct reference VB runtime (not high execution efficiency) Method: First, you need to add a reference code for VisualBasic.Runtime; information.isNumeric ("DDDDD");