导航:首页 > 编程语言 > 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是数字相关的资料

热点内容
如何将文件夹的内容发到微信 浏览:892
安卓手机emoji扁平怎么办 浏览:208
欧版三星s20怎么升级安卓11 浏览:104
我的世界服务器如何拉黑房子 浏览:480
linux服务器网络设置 浏览:106
在线编译代码怎么做 浏览:205
社会研究方法教程pdf 浏览:696
cad修改服务器地址 浏览:788
新手服务器装什么系统好 浏览:128
androidxmlui 浏览:713
星辰之恋在哪个app播 浏览:465
手机卡服务器出错误是怎么回事 浏览:536
cg编译器怎么设置 浏览:418
单片机教学视频哪个好 浏览:598
编译器错误了怎么办 浏览:77
看摩托车用什么app好 浏览:405
pdf转换excel在线转换 浏览:361
php多客服 浏览:746
语言编译程序如何分类 浏览:377
pdf下载哪个 浏览:77