会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
M'
.net 技术资料
博客园
首页
新随笔
联系
订阅
管理
C# 判断string 是否可为数字
//
正则表达式
//
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;
}
}
from:http://miaoshunping.bokee.com/5836813.html
posted @
2007-09-06 17:25
M'
阅读(
486
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告