I have this problem: I have a String
, but I need to make sure that it only contains letters A-Z and numbers 0-9. Here is my current code:
boolean valid = true;
for (char c : string.toCharArray()) {
int type = Character.getType(c);
if (type == 2 || type == 1 || type == 9) {
// the character is either a letter or a digit
} else {
valid = false;
break;
}
}
But what is the best and the most efficient way to implement it?
The Java String contains() method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement.
Read the String. Convert all the characters in the given String to lower case using the toLower() method. Convert it into a character array using the toCharArray() method of the String class. Find whether every character in the array is in between a and z, if not, return false.
StringUtils in Apache Commons Lang 3 has a containsOnly method, https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
The implementation should be fast enough.
Since no one else has worried about "fastest" yet, here is my contribution:
boolean valid = true;
char[] a = s.toCharArray();
for (char c: a)
{
valid = ((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9'));
if (!valid)
{
break;
}
}
return valid;
Full test code below:
public static void main(String[] args)
{
String[] testStrings = {"abcdefghijklmnopqrstuvwxyz0123456789", "", "00000", "abcdefghijklmnopqrstuvwxyz0123456789&", "1", "q", "test123", "(#*$))&v", "ABC123", "hello", "supercalifragilisticexpialidocious"};
long startNanos = System.nanoTime();
for (String testString: testStrings)
{
isAlphaNumericOriginal(testString);
}
System.out.println("Time for isAlphaNumericOriginal: " + (System.nanoTime() - startNanos) + " ns");
startNanos = System.nanoTime();
for (String testString: testStrings)
{
isAlphaNumericFast(testString);
}
System.out.println("Time for isAlphaNumericFast: " + (System.nanoTime() - startNanos) + " ns");
startNanos = System.nanoTime();
for (String testString: testStrings)
{
isAlphaNumericRegEx(testString);
}
System.out.println("Time for isAlphaNumericRegEx: " + (System.nanoTime() - startNanos) + " ns");
startNanos = System.nanoTime();
for (String testString: testStrings)
{
isAlphaNumericIsLetterOrDigit(testString);
}
System.out.println("Time for isAlphaNumericIsLetterOrDigit: " + (System.nanoTime() - startNanos) + " ns");
}
private static boolean isAlphaNumericOriginal(String s)
{
boolean valid = true;
for (char c : s.toCharArray())
{
int type = Character.getType(c);
if (type == 2 || type == 1 || type == 9)
{
// the character is either a letter or a digit
}
else
{
valid = false;
break;
}
}
return valid;
}
private static boolean isAlphaNumericFast(String s)
{
boolean valid = true;
char[] a = s.toCharArray();
for (char c: a)
{
valid = ((c >= 'a') && (c <= 'z')) ||
((c >= 'A') && (c <= 'Z')) ||
((c >= '0') && (c <= '9'));
if (!valid)
{
break;
}
}
return valid;
}
private static boolean isAlphaNumericRegEx(String s)
{
return Pattern.matches("[\\dA-Za-z]+", s);
}
private static boolean isAlphaNumericIsLetterOrDigit(String s)
{
boolean valid = true;
for (char c : s.toCharArray()) {
if(!Character.isLetterOrDigit(c))
{
valid = false;
break;
}
}
return valid;
}
Produces this output for me:
Time for isAlphaNumericOriginal: 164960 ns
Time for isAlphaNumericFast: 18472 ns
Time for isAlphaNumericRegEx: 1978230 ns
Time for isAlphaNumericIsLetterOrDigit: 110315 ns
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