java中判断一个字符串是否“都为数字”和“是否包含数字”和“截取数字”

时间:2025-02-14 19:58:15


// 判断一个字符串是否都为数字  
public boolean isDigit(String strNum) {  
    return ("[0-9]{1,}");  
}  
  
// 判断一个字符串是否都为数字  
public boolean isDigit(String strNum) {  
    Pattern pattern = ("[0-9]{1,}");  
    Matcher matcher = ((CharSequence) strNum);  
    return ();  
}

//截取数字  
public String getNumbers(String content) {  
    Pattern pattern = ("\\d+");  
    Matcher matcher = (content);  
    while (()) {  
       return (0);  
    }  
    return "";  
}  
  
// 截取非数字  
public String splitNotNumber(String content) {  
    Pattern pattern = ("\\D+");  
    Matcher matcher = (content);  
    while (()) {  
        return (0);  
    }  
    return "";  
}
  
// 判断一个字符串是否含有数字
public boolean HasDigit(String content) {
    boolean flag = false;
    Pattern p = (".*\\d+.*");
    Matcher m = (content);
    if (()) {
        flag = true;
    }
    return flag;
}



java中判断字符串是否为数字的三种方法 :

1.用JAVA自带的函数 

    public static boolean isNumeric(String str) {
        for (int i = (); --i >= 0;) {
            if (!((i))) {
                return false;
            }
        }
        return true;
    }


2.用正则表达式 

    public static boolean isNumeric(String str) {
        Pattern pattern = ("[0-9]*");
        return (str).matches();
    }


3.用ascii码 

    public static boolean isNumeric(String str) {
        for (int i = (); --i >= 0;) {
            int chr = (i);
            if (chr < 48 || chr > 57)
                return false;
        }
        return true;
    }