alpha-numeric String

What's the best way to check that a string is lower case and alpha-numeric?

I'm using

char[] ca = userName.toCharArray();

for (int i = 0; i < ca.length[]; i++)

{

if (!(ca[i] >= 48 && ca[i] <= 57) && !(ca[i] >= 97 && ca[i] <= 122))

{

// user name is not lower case alpha-numeric.

}

}

Is there a better way?

Thanks in advance,

Matt

[730 byte] By [mattbunch] at [2007-9-26 2:18:47]
# 1

I got this off someone last week for checking alphanumeric'ness'.

you could add your lowercase check to this.

I think this doesn't work when the string is just spaces(i.e it incorrectly returns true)

public static boolean isStringAllLettersOrDigits(String aString)

{

int stringSize = aString.length();

for (int i = 0; i < stringSize; i++)

{

boolean check = Character.isLetterOrDigit(aString.charAt(i));

if (!check)

{

return false;

}

};

return true;

}

javajono at 2007-6-29 9:20:16 > top of Java-index,Archived Forums,Java Programming...
# 2
You can also use regexp's. There are a few regexp packages available that will let you match the string to any regular expression.
dewangs at 2007-6-29 9:20:16 > top of Java-index,Archived Forums,Java Programming...