• Post Reply Bookmark Topic Watch Topic
  • New Topic

Test alphanumeric string  RSS feed

 
lanshu usav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to test an input string. It has to be 10 characters or less and each of the characters has to be alphanumeric(letter or digit). Request people here to suggest a way to do it. Is there a method available already in java api - something like testAlphaNumeric?

Thanks in advance
lanshu
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.length() gives you the length of a string.

Convert to a char array (String.toCharArray()) and then test each character in the array using the appropriate Character.isXXX() method.
[ August 18, 2005: Message edited by: Joanne Neal ]
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very simple using regular expressions -



Google for 'regex' and then check out the java.util.regex.Pattern class.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I just saw that you wanted 10 or less not just 10!



I assume that you must have at least 1 character!
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ianshu,
just to expand a little on what joanne said : heres some psuedo code

String word is equal to JOptionpane method

begin while

char is equal to each iteration character within word

if character is letter or digit

do something with character
end if
end while

exit system

HTH.
 
lanshu usav
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using regex is so simple but I think the JDK that we are using here is rather older and doesn't have regex.
I am parsing the string and checking each char.

Thanks all..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!