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

热点内容
公安主题网站源码 浏览:982
天津市服务器供应商云服务器 浏览:107
数控车床子程序编程 浏览:107
floydwarshall算法 浏览:715
丢失微信app怎么找 浏览:250
php能写前端吗 浏览:5
服务器如何更改raid模式 浏览:90
方舟服务器怎么导出来 浏览:608
手机显示服务器异常什么鬼 浏览:379
新闻服务器的网址是什么 浏览:669
程序员年底招人 浏览:319
广发app怎么查房贷 浏览:860
安卓手机怎么下薯仔 浏览:921
只有一个app显示网络异常怎么回事 浏览:988
解压玩具是水宝宝 浏览:817
压缩机保护怎么解决 浏览:944
单片机简易电子时钟 浏览:402
pdf影印版 浏览:689
单片机的中断技术 浏览:626
表格加密才能打开 浏览:39