zoukankan
html css js c++ java
验证输入的是否数字的几种方法
方法一:
static
bool
IsNumeric(
string
str)
{
if
(str
==
null
||
str.Length
==
0
)
return
false
;
foreach
(
char
c
in
str)
{
if
(
!
Char.IsNumber(c))
{
return
false
;
}
}
return
true
;
}
方法二:
private
bool
IsNumeric(
string
s)
{
char
ch0
=
'
0
'
;
char
ch9
=
'
9
'
;
for
(
int
i
=
0
; i
<
s.Length; i
++
)
{
if
((s[i]
<
ch0
||
s[i]
>
ch9))
{
this
.lblwarning.Text
=
"
此处应输入整数且非负!
"
;
return
false
;
}
}
return
true
;
}
方法三:
static
bool
IsNumeric (
string
str)
{
System.Text.RegularExpressions.Regex reg1
=
new
System.Text.RegularExpressions.Regex(
@"
^[-]?\d+[.]?\d*$
"
);
return
reg1.IsMatch(str);
}
方法四:(可扩展)
public
static
bool
IsConvert(
string
Expression,Type DataType)
{
switch
(DataType.Name)
{
case
"
Double
"
:
try
{
Double.Parse(Expression);
return
true
;
}
catch
{
return
false
;
}
case
"
DateTime
"
:
try
{
DateTime.Parse(Expression);
return
true
;
}
catch
{
return
false
;
}
default
:
return
true
;
}
}
正则表达的写法是:
static
bool
IsNumeric(
string
str)
{
System.Text.RegularExpressions.Regex reg1
=
new
System.Text.RegularExpressions.Regex(
@"
^[-]?\d+[.]?\d*$
"
);
return
reg1.IsMatch(str);
}
查看全文
相关阅读:
9.堆排序
8.全排列
37.微信跳一跳辅助开发(C语言+EasyX)
7.图形化实现快速排序法
codeforces 632A A. Grandma Laura and Apples(暴力)
codeforces 633D D. Fibonacci-ish(dfs+暴力+map)
codeforces 633B B. A Trivial Problem(数论)
codeforces 633A A. Ebony and Ivory(暴力)
codeforces 622B B. The Time
codeforces 622D D. Optimal Number Permutation(找规律)
原文地址:https://www.cnblogs.com/Magicam/p/1202068.html
最新文章
线索二叉树
优先级队列
在二元树中找出和为某一值的所有路径
定义一个栈的数据结构,要求实现一个min函数,每次能够得到栈的最小值,并且要求Min的时间复杂度为O(1)
poj3667Hotel
poj3667Hotel
bsgs(数论)
bzoj1101 [POI2007]Zap
bzoj1101 [POI2007]Zap
AC自动机总结
热门文章
poj1279Art Gallery
poj1279Art Gallery
bzoj1835基站选址(dp+线段树)
bzoj1835基站选址(dp+线段树)
40.多线程实现矩阵相乘
39.C语言操作数据库
38.C语言字符串总结
10.调用函数,快速排序法
3.cocos代码入口
37.cgi网页交互
Copyright © 2011-2022 走看看