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);
}
查看全文
相关阅读:
Codeforces Round #647 (Div. 2) A
Acwing 1129. 热浪 解题报告
二分 + 三分模板
可持久化线段树(主席树)模板
《架构之美》-----阅读笔记四
《架构之美》-----阅读笔记三
《架构之美》-----阅读笔记二
《架构之美》-----阅读笔记一
《软件需求》------阅读笔记三
《软件需求》------阅读笔记二
原文地址:https://www.cnblogs.com/Magicam/p/1202068.html
最新文章
利用NPOI解析Excel的通用类
GIS入门基础知识点
一、Redis数据备份与恢复
Hive QL的实例
Hive QL的操作
Hive的用法
“架构漫谈”读后感想
Myeclipse中各种Library的含义
<base href="<%=basePath%>">的理解
软件架构师的工作过程
热门文章
java中接口(interface)和虚基类(abstract class)的区别
《软件需求最佳实践》阅读笔记二
《软件需求最佳实践》阅读笔记一
软件需求与分析课堂讨论一
问题账户需求分析
Codeforces Round #670 (Div. 2) A
Codeforces Round #669 (Div. 2) A
Codeforces #666 div2 A
2020牛客暑期多校训练营(第一场)J-Easy Integration
AcWing 903. 昂贵的聘礼 解题报告
Copyright © 2011-2022 走看看