How to check if a String is numeric in Java

前端 未结 30 3526
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:26

How would you check if a String was a number before parsing it?

30条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 06:00

    To match only positive base-ten integers, that contains only ASCII digits, use:

    public static boolean isNumeric(String maybeNumeric) {
        return maybeNumeric != null && maybeNumeric.matches("[0-9]+");
    }
    

提交回复
热议问题