Last active
April 28, 2016 21:08
-
-
Save jluismm2311/45f50e5bb7e09c1a865ed7f84c7ef36d to your computer and use it in GitHub Desktop.
Implement String#hex_number? (in Java StringUtils.isHexNumber(String)), which should return true if given object is a hexadecimal number, false otherwise. Hexadecimal numbers consist of one or more digits from range 0-9 A-F (in any case), optionally prefixed by 0x.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class StringUtils { | |
public static boolean isHexNumber(String s) { | |
boolean b = false; | |
if(s != null){ | |
Pattern p = Pattern.compile("^(0x)?[\\dA-Fa-f]+$"); | |
Matcher m = p.matcher(s); | |
b = m.matches(); | |
} | |
return b; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.hexNumber = function() { | |
return /^(0x)?[\da-f]+$/i.test(this); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment