zoukankan
html css js c++ java
C#中如何判斷一個String是否為數字
Copied From
http://www.cnblogs.com/zm235/archive/2006/09/23/512742.html
方案一:TryCatch(执行效率不高)
private
bool
IsNumberic(
string
oText)
{
try
{
int
var1
=
Convert.ToInt32 (oText);
return
true
;
}
catch
{
return
false
;
}
}
方案二:正则表达式(推荐)
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*$
"
);
}
方案三:遍历
a)
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)
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)
public
static
bool
isNumeric(
string
inString)
{
inString
=
inString.Trim();
bool
haveNumber
=
false
;
bool
haveDot
=
false
;
for
(
int
i
=
0
;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源代码(执行效率不高)
//
主调函数
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;
}
//
子函数
//
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运行库(执行效率不高)
方法: 首先需要添加Visualbasic.runtime的引用
代码中Using Microsoft.visualbasic;
程序中用Information.isnumeric(
"
ddddd
"
);
查看全文
相关阅读:
-bash: belts.awk: command not found
PLS-00357: Table,View Or Sequence reference 'SEQ_TRADE_RECODE.NEXTVAL' not allowed in this context
初识makefile
proc:基本数据库操作
ORA-12154: TNS:could not resolve the connect identifier specified
简单的爬虫
合并一个文文件夹下的所有Excel文件
Python 递归读取文件夹内所有文件名(包含子文件夹)
CSS
JQ
原文地址:https://www.cnblogs.com/dimg/p/707338.html
最新文章
国王用金币奖励他忠诚的侍从,第一天侍从工作结束后,国王奖励了他一个金币; 接着的两天侍从工作中(第二天,第三天)的每一天,国王奖励了他两个金币;接着的三天 侍从工作中(第四、五、六天)的每一天,国王奖励了他三个金币; 要求在给定的天数时,程序能够统计国王共奖励了侍从多少个金币。
单链表相关操作
从键盘读取一个字符串,将字符串中所有连续的数字转换为对应的整数存储 到一个整数数组中,最后将这些整数输出
对于十进制数,数字符号使用阿拉伯数字0-9,对于超过十的进制,除了用0-9外, 还依次用字母A、B、C、D……表示数10、11、12、13……,下面的程序将一个以字符串 形式的K进制整数转换成十进制整数形式输出
//设计一个函数fun()将形参s所指字符串中重复的字符删除得只留下第一个,返回删除后字符串的指针
deepin系统中安装配置mysql5.7
deepin系统,apt源更换
c++类访问修饰符
c/c++逻辑运算符和位运算符
c++存储类
热门文章
mysql查看表结构、字段信息
蛇形打印
字符串转换成整数
const char*, char*const 的区别
实现单链表的逆置
final关键字
springboot打成war包找不到文件
IO流
编程例子:通过交易码获取交易名称(待解决)
二维数组与指针的一个例子
Copyright © 2011-2022 走看看