java 判斷一個字符串中的數字:是否為數字、是否包含數字、截取數字


題外話:

  JavaScript中判斷一個字符是否為數字,用函數:isDigit();

一、判斷一個字符串是否都為數字

package com.cmc.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DigitUtil {

    public static void main(String[] args) {
        String str="123d";
        System.out.println(isDigit2(str));
    }
    
    //判斷一個字符是否都為數字 
    public static boolean isDigit(String strNum){
        return strNum.matches("[0-9]{1,}");
    }
    
    // 判斷一個字符串是否都為數字  
    public static boolean isDigit2(String strNum) {  
        Pattern pattern = Pattern.compile("[0-9]{1,}");  
        Matcher matcher = pattern.matcher((CharSequence) strNum);  
        return matcher.matches();  
    }
}

 

二、判斷字符串中是否包含數字

// 判斷一個字符串是否含有數字
    public boolean HasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

 

三、截取字符串中的數字

package com.cmc.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DigitUtil {

    public static void main(String[] args) {
        String str="123dsd99";
        System.out.println(getNumbers(str));  //===>  123
System.out.println(splitNotNumber(str)); //===> dsd }
//截取數字 【讀取字符串中第一個連續的字符串,不包含后面不連續的數字】 public static String getNumbers(String content) { Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(content); while (matcher.find()) { return matcher.group(0); } return ""; } // 截取非數字 public static String splitNotNumber(String content) { Pattern pattern = Pattern.compile("\\D+"); Matcher matcher = pattern.matcher(content); while (matcher.find()) { return matcher.group(0); } return ""; } }

 

四、判斷字符串是否為數字的方法

  (一)用java自帶函數

    public static void main(String[] args) {
        String str="12399";
        System.out.println(isNumeric(str));
    }
    
    //判斷一個字符串是否為數字
    public static boolean isNumeric(String str){
        for (int i =  str.length(); --i>=0;) {
            if(!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

  (二)用正則表達式 

    public static void main(String[] args) {
        String str="123p99";
        System.out.println(isNumeric2(str));
    }
    
    public static boolean isNumeric2(String str){
        Pattern pattern=Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

  (三)用ASCII碼

    public static void main(String[] args) {
        String str="12399";
        System.out.println(isNumeric3(str));
    }
    
    //判斷一個字符串是否為數字
    public static boolean isNumeric3(String str){
        for(int i=str.length(); --i>=0;){
            char c=str.charAt(i);
            if (c < 48 || c > 57)
                return false;
        }
        return true;
    }

 


免責聲明!

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



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