zoukankan
html css js c++ java
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
查看全文
相关阅读:
[WCF安全系列]从两种安全模式谈起
为自定义配置的编辑提供”智能感知”的支持
在Entity Framework中使用存储过程(二):具有继承关系实体的存储过程如何定义?
[WCF安全系列]实例演示:TLS/SSL在WCF中的应用[HTTPS]
[WCF安全系列]谈谈WCF的客户端认证[Windows认证]
在Entity Framework中使用存储过程(三):逻辑删除的实现与自增长列值返回
[转] Leaving patterns & practices
两个简单的扩展方法:TrimPrefix和TrimSuffix
Oracle 系统表
让IoC动态解析自定义配置(提供基于Unity的实现)
原文地址:https://www.cnblogs.com/yiki/p/884643.html
最新文章
Atlas学习手记(9):异步调用Page Method
说说雅虎收藏+
Atlas学习手记(12):使用CascadingDropDown控件
Atlas学习手记(5):使用服务端定时控件TimerControl
Atlas学习手记(13):使用TextBoxWatermark为TextBox加上水印效果
Atlas学习手记(8):调用本地Web Service简单介绍
介绍Postman的Mock Server和Easy Mock
关于Expression Tree和IL Emit的所谓的"性能差别"
一个关于ConfigurationManager.GetSecion方法的小问题
[WCF安全系列]绑定、安全模式与客户端凭证类型:总结篇
热门文章
在Entity Framework中使用存储过程(五):如何通过存储过程维护多对多关系?
[WCF安全系列]绑定、安全模式与客户端凭证类型:BasicHttpBinding
[WCF安全系列]谈谈WCF的客户端认证[X.509证书认证]
[WCF安全系列]认证与凭证:用户名/密码认证与Windows认证
从Trace和Debug来看条件编译(Conditional Compilation)
晚绑定场景下对象属性赋值和取值可以不需要PropertyInfo
[WCF安全系列]绑定、安全模式与客户端凭证类型:NetNamedPipeBinding、NetTcpBinding与NetMsmqBinding
[WCF安全系列]认证与凭证:X.509证书
[WCF安全系列]消息的保护等级[下篇]
[WCF安全系列]谈谈WCF的客户端认证[用户名/密码认证]
Copyright © 2011-2022 走看看