java從字符串中提取數字


轉:

java從字符串中提取數字

 

    1. // 判斷一個字符串是否都為數字  
    2. public boolean isDigit(String strNum) {  
    3.     return strNum.matches("[0-9]{1,}");  
    4. }  
    5.   
    6. // 判斷一個字符串是否都為數字  
    7. public boolean isDigit(String strNum) {  
    8.     Pattern pattern = Pattern.compile("[0-9]{1,}");  
    9.     Matcher matcher = pattern.matcher((CharSequence) strNum);  
    10.     return matcher.matches();  
    11. }  
    12.   
    13.    //截取數字  
    14.    public String getNumbers(String content) {  
    15.        Pattern pattern = Pattern.compile("\\d+");  
    16.        Matcher matcher = pattern.matcher(content);  
    17.        while (matcher.find()) {  
    18.            return matcher.group(0);  
    19.        }  
    20.        return "";  
    21.    }  
    22.   
    23. // 截取非數字  
    24. public String splitNotNumber(String content) {  
    25.     Pattern pattern = Pattern.compile("\\D+");  
    26.     Matcher matcher = pattern.matcher(content);  
    27.     while (matcher.find()) {  
    28.         return matcher.group(0);  
    29.     }  
    30.     return "";  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM