Verify if String is hexadecimal

后端 未结 7 1156
花落未央
花落未央 2020-12-03 06:39

I have a String like \"09a\" and I need a method to confirm if the text is hexadecimal. The code I\'ve posted does something similar, it verifies that a string is a decimal

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 07:13

    Horrible abuse of exceptions. Don't ever do this! (It's not me, it's Josh Bloch's Effective Java). Anyway, I suggest

    private static final Pattern HEXADECIMAL_PATTERN = Pattern.compile("\\p{XDigit}+");
    
    private boolean isHexadecimal(String input) {
        final Matcher matcher = HEXADECIMAL_PATTERN.matcher(input);
        return matcher.matches();
    }
    

提交回复
热议问题