In any of the standard libraries is there a definition for characters classes (alpha, numeric, alphanumeric)? I'm checking if a string contains only alphanumeric characters or a colon:
StringUtils.containsOnly(input, ALPHA_NUMERIC + ":");
I could define ALPHA_NUMERIC myself, but it seems common characters classes would be defined in a standard library, although I have been unable to find the definitions.
edit: I did consider regex, but for my particular use case execution time is important, and a simple scan is more efficient.
edit: Here are the test results, using Regex, CharMatcher, and a simple scan (using the same set of valid/invalid input strings for each test):
Valid Input Strings:
CharMatcher, Num Runs: 1000000, Valid Strings: true, Time (ms): 1200
Regex, Num Runs: 1000000, Valid Strings: true, Time (ms): 909
Scan, Num Runs: 1000000, Valid Strings: true, Time (ms): 96
Invalid input strings:
CharMatcher, Num Runs: 1000000, Valid Strings: false, Time (ms): 277
Regex, Num Runs: 1000000, Valid Strings: false, Time (ms): 253
Scan, Num Runs: 1000000, Valid Strings: false, Time (ms): 36
Here is the code that performed the scan:
public boolean matches(String input) {
for(int i=0; i<input.length(); i++) {
char c = input.charAt(i);
if( !Character.isLetterOrDigit(c) && c != ':') {
return false;
}
}
return true;
}
edit: I recompiled as a standalone program (I was running through eclipse):
CharMatcherTester, Num Runs: 1000000, Valid Strings: true, Time (ms): 418
RegexTester, Num Runs: 1000000, Valid Strings: true, Time (ms): 812
ScanTester, Num Runs: 1000000, Valid Strings: true, Time (ms): 88
CharMatcherTester, Num Runs: 1000000, Valid Strings: false, Time (ms): 142
RegexTester, Num Runs: 1000000, Valid Strings: false, Time (ms): 223
ScanTester, Num Runs: 1000000, Valid Strings: false, Time (ms): 32
Source: https://bitbucket.org/jdeveloperw/testing (This is my first time posting test results to SO, so comments are appreciated.)
Your best bet is probably a regex Pattern.
It should match:
[\p{Alnum}:]*
\p{Alnum} - ASCII alphanumeric[] - character class (any of the characters inside will match one character): - literal :* - 0 or moreif it is all alphanumeric (or :).
You can use matches or pre-compile the regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With