Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
Last active April 28, 2016 21:08
Show Gist options
  • Save jluismm2311/45f50e5bb7e09c1a865ed7f84c7ef36d to your computer and use it in GitHub Desktop.
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.
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;
}
}
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