1. Regular Expression
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 * $ ");
}
2. Traverse
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 |