Java判断字符是否是数字

Java判断字符是否是数字

package com.example.core.mydemo.javatest;

/**
 * 判断是否是数字
 */
public class TestInt {
    public static void main(String[] args) {
        System.out.println("isNumeric1=" + isNumeric("11"));
        System.out.println("isNumeric2=" + isNumeric("aa"));

    }

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


}

 output:

isNumeric1=true
isNumeric2=false

posted on 2025-01-23 16:32  oktokeep  阅读(11)  评论(0)    收藏  举报