Java中判断,一个字符串是否全是数字

Java中判断,一个字符串是否全是数字

总代码:

九种方法,总有几个是看得懂的。
代码前三个用了Java自带的方法,后部分用的都是正则表达式。

package com.guo;

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

public class Test {
    public static void main(String[] args) {

        System.out.println(isNum3("str324234"));
        System.out.println(isNum3("ew2345errw"));
        System.out.println(isNum3("43535334"));
        System.out.println(isNum3("34"));
        System.out.println(isNum3("3423_2313"));
        System.out.println(isNum3("12水56"));
        System.out.println(isNum3("-123456"));
        System.out.println(isNum3("123456"));

    }


    public static boolean isNum(String str){
    	if (str.length() == 0 || str == null)
            return false;
        for (int i = 0; i < str.length(); i++){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

    public static boolean isAllNumber(String str){
        char[] data= str.toCharArray();
        for (int i = 0; i <data.length ; i++) {
            char c=data[i];
            // 拆字符用的挺多,好记还灵活,就是麻烦
            // 判断字符是否为纯数字和字母
            //  if (c<‘0’||c>‘9’ && c<‘A’||c>‘Z’ && c<‘a’||c>‘a’)
            if(c<'0'|| c>'9'){
                return false;
            }
        }
        return true;
    }

    private static boolean isNumString(String str) {
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (!(ch >= '0' && ch <= '9')) {
                return false;
            }
        }
        return true;
    }

    
    // 以下是正则表达式部分
    public static boolean isDigit1(String str) {
        if (str == null)
            return false;
        return str.matches("[0-9]{1,}");
    }

    public static boolean isDigit2(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher(strNum);
        return matcher.matches();
    }

    public static boolean isNum1(String strNum) {
        //Pattern pattern = Pattern.compile("[0-9]*"); // 此方法只验证正数
        //Pattern pattern = Pattern.compile("-[0-9]*"); // 此方法只验证负数
        Pattern pattern = Pattern.compile("-?[0-9]*"); // 验证数字
        // Pattern pattern = Pattern.compile("-?[0-9]+?[0-9]+");(与上一行同效果)

        Matcher matcher = pattern.matcher(strNum);
        return matcher.matches();
    }

    public static boolean isNum2(String str){
        return str.matches("[0-9]+");
    }

    public static boolean isNum3(String str) {
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        return pattern.matcher(str).matches();
    }

    // 与isNum1效果相同
    public static boolean isNum4(String str) {
        Pattern pat = Pattern.compile("^-?[0-9]+$");
        Matcher mat = pat.matcher(str);
        if (mat.find()) {
            return true;
        } else {
            return false;
        }
    }

}

运行结果:

false
false
true
true
false
false
true
true

Java自带的方法:

使用了Character(char的包装类)的方法

public static boolean isNum(String str){
		if (str.length() == 0 || str == null)
            return false;
        for (int i = 0; i < str.length(); i++){
            if (!Character.isDigit(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

将字符串拆成字符

	// 将字符串一次性拆分为字符型数组,判断数组里的字符是不是数字
	public static boolean isAllNumber(String str){
        char[] data= str.toCharArray();
        for (int i = 0; i <data.length ; i++) {
            char c=data[i];
            // 拆字符用的挺多,好记还灵活,就是麻烦
            // 判断字符是否为纯数字和字母
            // if (c<‘0’||c>‘9’ && c<‘A’||c>‘Z’ && c<‘a’||c>‘a’)
            // if(chr<48 || chr>57) 跟下面一行同效果 
            if(c<'0'|| c>'9'){
                return false;
            }
        }
        return true;
    }

	// 将字符串逐个拆分,依次判断拆分而来的字符是不是数字
    private static boolean isNumString(String str) {
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (!(ch >= '0' && ch <= '9')) {
                return false;
            }
        }
        return true;
    }

正则表达式:

正则表达式不是很懂。。但它执行效率高。

public static boolean isDigit1(String str) {
        if (str == null)
            return false;
        return str.matches("[0-9]{1,}");
    }

    public static boolean isDigit2(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher(strNum);
        return matcher.matches();
    }

    public static boolean isNum1(String strNum) {
        //Pattern pattern = Pattern.compile("[0-9]*"); // 此方法只验证正数
        //Pattern pattern = Pattern.compile("-[0-9]*"); // 此方法只验证负数
        Pattern pattern = Pattern.compile("-?[0-9]*"); // 验证数字
        // Pattern pattern = Pattern.compile("-?[0-9]+?[0-9]+");(与上一行同效果)

        Matcher matcher = pattern.matcher(strNum);
        return matcher.matches();
    }

    public static boolean isNum2(String str){
        return str.matches("[0-9]+");
    }

    public static boolean isNum3(String str) {
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        return pattern.matcher(str).matches();
    }

    // 与isNum1效果相同
    public static boolean isNum4(String str) {
        Pattern pat = Pattern.compile("^-?[0-9]+$");
        Matcher mat = pat.matcher(str);
        if (mat.find()) {
            return true;
        } else {
            return false;
        }
    }
  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值