C#判斷一個(gè)String是否為數(shù)字類型
方案一:Try...Catch(執(zhí)行效率不高)
{
try
{
int var1=Convert.ToInt32 (oText);
return true;
}
catch
{
return false;
}
}
方案二:正則表達(dá)式(推薦)
a)
{
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)
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);
}
方案三:遍歷
a)
{
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)
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)
{
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;
}
方案四:改寫vb的IsNumeric源代碼(執(zhí)行效率不高)
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;
}//子函數(shù)
// 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;
}
方案五:直接引用vb運(yùn)行庫(執(zhí)行效率不高)
方法:首先需要添加Visualbasic.runtime的引用
代碼中Using Microsoft.visualbasic;
程序中用Information.isnumeric("ddddd");
以上就是C#判斷一個(gè)String是否為數(shù)字類型的全部內(nèi)容,推薦大家使用正則表達(dá)式的方法,比較簡單且效率高。
- c# StringBuilder.Replace 方法 (Char, Char, Int32, Int32)
- C# char[]與string byte[]與string之間的轉(zhuǎn)換詳解
- C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
- C#中dotnetcharting的用法實(shí)例詳解
- C#語言中字符類char的使用方法(總結(jié))
- C# char類型字符轉(zhuǎn)換大小寫的實(shí)現(xiàn)代碼
- C#中List〈string〉和string[]數(shù)組之間的相互轉(zhuǎn)換
- C#中string.format用法詳解
- C#、.Net中把字符串(String)格式轉(zhuǎn)換為DateTime類型的三種方法
- C#中char和string的入門使用教程
相關(guān)文章
C# 實(shí)現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼
這篇文章主要介紹了C# 實(shí)現(xiàn)TXT文檔轉(zhuǎn)Table的示例代碼,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2020-12-12
如何用C#獲取計(jì)算機(jī)詳細(xì)的軟件和硬件信息
我們應(yīng)該都知道System.Management提供的類可以用于讀取本地計(jì)算機(jī)設(shè)備的各種數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于如何用C#獲取計(jì)算機(jī)詳細(xì)的軟件和硬件信息的相關(guān)資料,需要的朋友可以參考下2022-12-12
C#?wpf使用DockPanel實(shí)現(xiàn)制作截屏框
做桌面客戶端的時(shí)候有時(shí)需要實(shí)現(xiàn)截屏功能,能夠在界面上框選截屏,本文就來為大家介紹一下wpf如何使用DockPanel制作截屏框吧,感興趣的可以了解下2023-09-09
C#實(shí)現(xiàn)拷貝文件到另一個(gè)文件夾下
這篇文章主要介紹了C#實(shí)現(xiàn)拷貝文件到另一個(gè)文件夾下,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
C#搜索TreeView子節(jié)點(diǎn),保留父節(jié)點(diǎn)的方法
這篇文章主要介紹了C#搜索TreeView子節(jié)點(diǎn),保留父節(jié)點(diǎn)的方法,實(shí)例分析了C#操作TreeView節(jié)點(diǎn)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
解決C# winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法詳解
本篇文章是對(duì)在C#中winForm自定義鼠標(biāo)樣式的兩種實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
輕松學(xué)習(xí)C#的結(jié)構(gòu)和類
輕松學(xué)習(xí)C#的結(jié)構(gòu)和類,對(duì)C#的結(jié)構(gòu)和類感興趣的朋友可以參考本篇文章,幫助大家更靈活的運(yùn)用C#的結(jié)構(gòu)和類2015-11-11
c++函數(shù)轉(zhuǎn)c#函數(shù)示例程序分享
這篇文章主要介紹了c++函數(shù)轉(zhuǎn)c#函數(shù)示例程序,大家參考使用吧2013-12-12

