java中判断字符串是否为数字的三种方法
java中判断字符串是否为数字的三种方法
1>用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
2>用正则表达式
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
3>用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
4>用异常
public static boolean isNumeric(String str){
try{
Integer.parseInt(str);
}
catch(NumberFormatException ne){
return false;
}
return true;
}
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
本文讲述程序开发者怎样使用NetBeans 6.8 IDE和JavaFX技术创建他们的第一个JavaFX应用程序。在文章中,我们将创建一个简单的带有文本的球体。该球体在一个特定的时间周期内改变其透明度。你还可以使用鼠标拖动球体。
同样的原因,因为文内有很多操作截图,这里插入很不方便, ......
for(int i=0;i<string.length();i++)
{
char x=string.CharAt(i);
if(Character.isDigit(i)==true){
//x类型转换然后统计
}
}
如下可以将字母与数字分离出来
用正则!
String str="200Minute";
String str2="300.25Hour";
String regex="[a-zA-Z]" ......