首页 > 编程语言 > 详细

java 判断一个字符串中的数字:是否为数字、是否包含数字、截取数字

时间:2017-04-28 13:30:25      阅读:148      评论:0      收藏:0      [点我收藏+]

题外话:

  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;
    }

 

java 判断一个字符串中的数字:是否为数字、是否包含数字、截取数字

原文:http://www.cnblogs.com/renxiaoren/p/6780707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!