Verify if String is hexadecimal

后端 未结 7 1191
花落未央
花落未央 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:21

    Used this in my own code to check if string is a MAC address

    boolean isHex = mac_addr.matches("^[0-9a-fA-F]+$");
    

    My beef with the other answers provided in this thread is that if the String's length is large, it would throw an exception as well. Therefore, not very useful if you are testing if a MAC address consist of valid hexadecimals.

    Don't be terrified of using regex!

提交回复
热议问题