C # To determine whether a string is a numeric type _c# tutorial

Source: Internet
Author: User
Tags numeric

Scheme one: Try ... Catch (Inefficient execution)

Copy Code code as follows:
private bool Isnumberic (string otext)
{
Try
{
int Var1=convert.toint32 (otext);
return true;
}
Catch
{
return false;
}
}

Scenario Two: Regular Expressions (recommended)

A

Copy Code code as follows:
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*$ ");
}

b

Copy Code code as follows:
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);
}

Scenario Three: Traversal

A

Copy Code code as follows:
public bool IsNumeric (string str)
{
Char[] Ch=new char[str. Length];
Ch=str. ToCharArray ();
for (int i=0;i {
if (ch[i]<48 | | ch[i]>57)
return false;
}
return true;
}

b

Copy Code code as follows:
public bool Isinteger (string strin) {
BOOL Bolresult=true;
if (strin== "") {
Bolresult=false;
}
else {
foreach (char char in Strin) {
if (char. Isnumber (Char))
Continue
else {
Bolresult=false;
Break
}
}
}
return bolresult;
}

C

Copy Code code as follows:
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;
}

Scheme four: Rewrite the VB isnumeric source code (execution efficiency is not high)

Copy Code code as follows:
Keynote 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;
}//Child 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 < num2)
{
Char ch1 = value[num1];
if (ch1 = = ' & ')
{
Ch1 = char. ToLower (VALUE[NUM1 + 1], cultureinfo.invariantculture);
String Text1 = Stringtype.tohalfwidthnumbers (value.substring (NUM1 + 2));
if (ch1 = = ' h ')
{
I64value = Convert.toint64 (Text1, 0x10);
}
else if (ch1 = = ' O ')
{
I64value = Convert.toint64 (Text1, 8);
}
Else
{
throw new FormatException ();
}
return true;
}
if ((Ch1!= ') && (ch1!= '/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;
}

Scenario five: Directly referencing VB runtime (inefficient execution)

Method: First you need to add a visualbasic.runtime reference
The using Microsoft.VisualBasic in the code;
The procedure uses Information.isnumeric ("ddddd");

The above is C # to determine whether a string is the entire content of a numeric type, recommend the use of regular expressions of the method , relatively simple and high efficiency.

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.