导航:首页 > 编程语言 > java判断string是数字

java判断string是数字

发布时间:2022-09-20 05:30:39

java中怎样判断一个字符串是否是数字

用java的异常机制,不仅可以判断是否是数字,还可以判断整数或者小数:
public void checkInt(String bh){
try{
int num = Integer.parseInt(bh);//将输入的内容转换成int
System.out.println("是整数:"+num);//是整数
}catch (NumberFormatException e) {//转换成int类型时失败
try{
double d =Double.parseDouble(bh);//转成double类型
System.out.println("是小数:"+d);//是小数
}catch (NumberFormatException e2) {//转成double类型失败
System.out.println("不是数字");
}
}
}

② java中判断字符串是否为数字的方法的几种方法

可以使用正则表达式+中文字符编码区间验证一个字符串中是否包含汉字
代码如下:
public
static
void
main(string[]
args)
{
int
count
=
0;
string
regex
=
"[\\u4e00-\\u9fa5]";
//system.out.println(regex);
string
str
=
"中文fdas
";
//system.out.println(str);
pattern
p
=
pattern.compile(regex);
matcher
m
=
p.matcher(str);
while
(m.find())
{
for
(int
i
=
0;
i
<=
m.groupcount();
i++)
{
count
=
count
+
1;
}
}
system.out.println("共有
"
+
count
+
"个
");
}

③ java判断string是不是数字

常见的方法为正则判断,也可以使用char.isDigit(Char c)方法。

示例:

//方法一:正则
publicbooleanisNumerics(Stringstr){
if(!str.matches("[0-9]+")){
returnfalse;
}else{
returntrue;
}

}

//方法二:isDigit()
publicbooleanisNumeric(Stringstr){
for(inti=0;i<str.length();i++){
System.out.println(str.charAt(i));
if(!Character.isDigit(str.charAt(i))){
returnfalse;
}
}
returntrue;
}

④ Java中怎样判断一个字符串是否是数字

ava中判断字符串是否为数字的方法:

1.用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}

2.用正则表达式
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}

3.使用org.apache.commons.lang
org.apache.commons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
http://jakarta.apache.org/commons/lang/api-release/index.html下面的解释:
isNumeric
public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null will return false. An empty String ("") will return true.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false

Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null

上面三种方式中,第二种方式比较灵活。

第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;

而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。

阅读全文

与java判断string是数字相关的资料

热点内容
vb组态软件源码 浏览:698
加密蚊帐防蚊门帘 浏览:828
vs2010多线程编程 浏览:214
新股上市指标源码 浏览:698
c语言51单片机教程 浏览:554
app中的hd是什么意思 浏览:279
手机什么解压器最好 浏览:49
什么是适合自己的app 浏览:451
云服务器怎么补漏洞 浏览:176
如何看自己的安卓是什么等级 浏览:633
被丁磊挖走的程序员 浏览:19
gsk980ta编程 浏览:507
单片机离线 浏览:84
解压助眠敲击外国长指甲 浏览:421
3次元编程 浏览:803
夫妻传送英文命令 浏览:316
程序员题代做 浏览:950
戴尔服务器怎么扩大内存 浏览:641
解压包密码消失 浏览:135
昆仑通态触摸屏用户窗加密 浏览:564