Java 中怎样判断一个字符串全是数字 如何判断一个字符串里是否全是数字

Java\u4e2d\u600e\u6837\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u662f\u6570\u5b57\uff1f

\u7528\u6b63\u5219\u8868\u8fbe\u5f0f
public static boolean isNumericzidai(String str) {Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");Matcher isNum = pattern.matcher(str); if (!isNum.matches()) { return false;} return true;}12345678
\u7f51\u4e0a\u7ed9\u51fa\u7684\u6700\u597d\u7684\u65b9\u6cd5\uff0c\u53ef\u60dc\u8fd8\u662f\u9519\u8bef\uff1b\u9996\u5148\u6b63\u5219\u8868\u8fbe\u5f0f-?[0-9]+.?[0-9]+\u8fd9\u91cc\u5c31\u9519\u8bef \u7f51\u4e0a\u8bf4\uff1a\u53ef\u5339\u914d\u6240\u6709\u6570\u5b57\u3002 \u6bd4\u5982\uff1a
double aa = -19162431.1254;String a = "-19162431.1254";String b = "-19162431a1254";String c = "\u4e2d\u6587";System.out.println(isNumericzidai(Double.toString(aa)));System.out.println(isNumericzidai(a));System.out.println(isNumericzidai(b));System.out.println(isNumericzidai(c));12345678
\u7ed3\u679c
falsetruetruefalse1234
\u6b63\u786e\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\uff1a-?[0-9]+\\.?[0-9]*\uff0c\u70b9\u53f7.,\u662f\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff0c\u9700\u8981\u8fdb\u884c\u8f6c\u4e49\u3002

Java\u4e2d\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5168\u662f\u6570\u5b57\uff1a
\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a


public boolean isNumeric(String str) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}


\u4f46\u662f\u8fd9\u4e2a\u65b9\u6cd5\u5e76\u4e0d\u5b89\u5168\uff0c\u6ca1\u6709\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u7a7a\u6821\u9a8c\u3002
\u5728\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\u5f88\u5bb9\u6613\u629b\u51fa\u5f02\u5e38\u3002
\u4f8b\u5982\u6267\u884c\uff1a


public static void main(String[] args) {

String str = null;
System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));

}


\u5c31\u4f1a\u629b\u51fa\u5f02\u5e38\uff1a


Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1140)
at java.util.regex.Matcher.reset(Matcher.java:291)
at java.util.regex.Matcher.(Matcher.java:211)
at java.util.regex.Pattern.matcher(Pattern.java:888)
at com.ossez.bcu.util.BarcodeChecksum.isNumeric(BarcodeChecksum.java:37)
at com.ossez.bcu.util.BarcodeChecksum.main(BarcodeChecksum.java:53)


\u6240\u4ee5\u8fd9\u4e2a\u65b9\u6cd5\u5e76\u4e0d\u51c6\u786e\u3002

\u5982\u679c\u6267\u884c\uff1a


public static void main(String[] args) {
String str = "";
System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));
}


\u5c06\u4f1a\u8fd4\u56de true\u3002
\u8fd9\u8bf4\u660e\u8fd9\u4e2a\u65b9\u6cd5\u6ca1\u6709\u5bf9\u7a7a\u5b57\u7b26\u4e32\u8fdb\u884c\u6821\u9a8c\u3002
\u53ef\u4ee5\u4f7f\u7528 Apache \u7684 StringUtils.isNumeric() \u51fd\u6570\u8fdb\u884c\u5224\u65ad\u3002
\u8fd9\u4e2a\u51fd\u6570\u4f4d\u4e8e org.apache.commons.lang.StringUtils; \u4e2d\u3002
\u4f46\u662f\uff0c\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679c\u4f20\u5165\u53c2\u6570\u4e3a "" \u540c\u6837\u4e5f\u4f1a\u4f60\u5b58\u5728\u5224\u65ad\u4e0d\u51c6\u786e\u7684\u60c5\u51b5\uff0c\u8fd9\u65f6\u5019\u9700\u8981\u9996\u5148\u5bf9\u9700\u8981\u8fdb\u884c\u5224\u65ad\u7684\u53c2\u6570\u8fdb\u884c\u975e\u7a7a\u6821\u9a8c\uff0c\u7136\u540e\u5220\u9664\u4f20\u5165\u6570\u636e\u4e2d\u7684\u7a7a\u683c\u3002


public static void main(String[] args) {
String str = "";
System.out.println(StringUtils.isNumeric(str));
}


\u4e0a\u9762\u8fd9\u4e2a\u51fd\u6570\u5c06\u4f1a\u8fd4\u56de true\u3002

Java中判断字符串是否全是数字:

可以使用正则表达式:

public boolean isNumeric(String str) {  
        Pattern pattern = Pattern.compile("[0-9]*");  
        Matcher isNum = pattern.matcher(str);  
        if (!isNum.matches()) {  
            return false;  
        }  
        return true;  
    }

但是这个方法并不安全,没有对字符串进行空校验。 
在程序执行的时候很容易抛出异常。 
例如执行:  

public static void main(String[] args) {  
      
        String str = null;  
        System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));  
  
    }

就会抛出异常:

Exception in thread "main" java.lang.NullPointerException  
    at java.util.regex.Matcher.getTextLength(Matcher.java:1140)  
    at java.util.regex.Matcher.reset(Matcher.java:291)  
    at java.util.regex.Matcher.<init>(Matcher.java:211)  
    at java.util.regex.Pattern.matcher(Pattern.java:888)  
    at com.ossez.bcu.util.BarcodeChecksum.isNumeric(BarcodeChecksum.java:37)  
    at com.ossez.bcu.util.BarcodeChecksum.main(BarcodeChecksum.java:53)

所以这个方法并不准确。 

如果执行:

public static void main(String[] args) {  
        String str = "";  
        System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));  
    }

将会返回 true。 
这说明这个方法没有对空字符串进行校验。 
可以使用 Apache 的 StringUtils.isNumeric() 函数进行判断。 
这个函数位于 org.apache.commons.lang.StringUtils;  中。 
但是,需要注意,如果传入参数为 "" 同样也会你存在判断不准确的情况,这时候需要首先对需要进行判断的参数进行非空校验,然后删除传入数据中的空格。

public static void main(String[] args) {  
        String str = "";  
        System.out.println(StringUtils.isNumeric(str));  
    }

上面这个函数将会返回 true。 
请先 trim 数据



方法有很多,个人觉得使用异常捕捉非常方便,上代码:

public static boolean isNumeric(String str){//捕获NumberFormatException异常

if (str != null) str = str.trim();//" 3 "自动去空格再判断

try{

Integer.parseInt(str);

return true;

}catch(NumberFormatException e){

System.out.println("异常:\"" + str + "\"不是数字/整数...");

return false;

}

}



String info = "";
try {
Integer.valueOf(info);
System.out.println("都是数字");
} catch (Exception e) {
System.out.println("有不是数字的字符");
}

这样子



判断字符串是否为数字:
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 ;
if(chr<48 || chr>57)
return false;
}
return true;
}

扩展阅读:java判断输入字符串 ... java中ascii码转换 ... javascript入门 ... java replace替换多个字符 ... java windowbuilder ... java特殊字符处理 ... java 怎么输出汉字 ... java中 符号 ... java中一个字符占几个字节 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网