Determines whether a string can be a number.

Source: Internet
Author: User

Solution 1: Try... catch (inefficient execution)
/// <Summary>
/// Name: isnumberic
/// Function: determines whether the input is a number.
/// Parameter: String otext: Source Text
/// Return value: bool true: false: No
/// </Summary>
/// <Param name = "otext"> </param>
/// <Returns> </returns>
Private bool isnumberic (string otext)
{
Try
{
Int var1 = convert. toint32 (otext );
Return true;
}
Catch
{
Return false;
}
}

Solution 2: Regular Expressions (recommended)
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 * $ ");
}
Solution 3: traverse
A)
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 | ch [I]> 57)
Return false;
}
Return true;
}

B)
Public bool isinteger (string strin ){
Bool bolresult = true;
If (strin = ""){
Bolresult = false;
}
Else {
Foreach (char in strin ){
If (char. isnumber (char ))
Continue;
Else {
Bolresult = false;
Break;
}
}
}
Return bolresult;
}

C)
Public static bool isnumeric (string instring)
{
Instring = instring. Trim ();
Bool havenumber = false;
Bool havedot = false;
For (INT I = 0; I <instring. length; I ++)
{
If (char. isnumber (instring [I])
{
Havenumber = true;
}
Else if (instring [I] = '.')
{
If (havedot)
{
Return false;
}
Else
{
Havedot = true;
}
}
Else if (I = 0)
{
If (instring [I]! = '+' & Instring [I]! = '-')
{
Return false;
}
}
Else
{
Return false;
}
If (I> 20)
{
Return false;
}
}
Return havenumber;
}
}

Solution 4: rewrite the isnumeric source code of VB (inefficient execution)

// Main 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 (char []) expression );
}
Else
{
Return false;
}
}
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;
}

// Subfunction
// 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 <num2)
{
Char numeric = value [num1];
If (response = '&')
{
Values = Char. tolower (value [num1 + 1], cultureinfo. invariantculture );
String text1 = stringtype. tohalfwidthnumbers (value. substring (num1 + 2 ));
If (Bytes = 'H ')
{
I64value = convert. toint64 (text1, 0x10 );
}
Else if (else = 'O ')
{
I64value = convert. toint64 (text1, 8 );
}
Else
{
Throw new formatexception ();
}
Return true;
}
If (response! = '') & (Success! = '\ U3000 '))
{
Return false;
}
Num1 ++;
}
Return false;
}
//----------------------------------------------------
// Doubletype. tryparse (text1, ref num1 );
Internal static bool tryparse (string value, ref double result)
{
Bool flag1;
Cultureinfo info1 = utils. getcultureinfo ();
Numberformatinfo info3 = info1.numberformat;
Numberformatinfo info2 = decimaltype. getnormalizednumberformat (info3 );
Value = stringtype. tohalfwidthnumbers (value, info1 );
If (info3 = info2)
{
Return double. tryparse (value, numberstyles. Any, info2, out result );
}
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;
}

Solution 5: directly reference the VB Runtime Library (low execution efficiency)

Method: Add a reference to VisualBasic. runtime.
Using Microsoft. VisualBasic;
Information. isnumeric ("ddddd") is used in the program ");

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.