-
June 21st, 2001, 09:05 AM
#1
Is there a way to check if a string is "alphanumeric" ?
Hi everybody,
Is there a way to check if a string is alphanumeric...I don't want to go througth character by
character using ..IsDigit or IsCharacter etc....
Thanks in advance,
-
June 21st, 2001, 09:41 AM
#2
Re: Is there a way to check if a string is "alphanumeric" ?
Write a method that goes through and checks isDigit or isCharacter and use that?
Seriously, there's no algorithm that can tell you that without visiting every value.
You could possibly find an algorithm that's faster if you only concern yourself with ascii, but otherwise,
you're stuck.
*adds isAlphaNumeric and isWord to his String library*
Bayard
bayard@generationjava.com
Brainbench MVP for Java
http://www.brainbench.com
-
June 22nd, 2001, 06:18 PM
#3
Re: Is there a way to check if a string is "alphanumeric" ?
This little trick applies if your String is small:
try
{
Long.parseLong(myString);
}
catch(NumberFormatException nfe)
{
// myString is not a pure number
}
Although I am not a big fan of using exceptions as messages/conditions but in this case looks like this is the only way.
-Tiwari
-
June 23rd, 2001, 11:38 AM
#4
Re: Is there a way to check if a string is "alphanumeric" ?
This only checks if a String is numeric.
I can't see any way in which the algorithm it uses could be faster than a simple loop over a char[], checking isDigit.
With one exception, Long.parseLong probably only checks ascii numbers. So a chinese number will return false.
Your choice as to whether that is necessary, but it's something you can put in your own method too.
parseLong might be implemented natively, which will give a speed increase, but the exception handling for a false condition will incur a high performance deficit.
Bayard
bayard@generationjava.com
Brainbench MVP for Java
http://www.brainbench.com
-
January 28th, 2005, 10:10 AM
#5
Re: Is there a way to check if a string is "alphanumeric" ?
I've done it this way:
Dim strWord as String
If (strWord Like "*[!A-Za-z]*") then
'strWord contains something other than A-Z or a-z
endif
You could also add the digits in for alphanumeric:
(strWord Like "*[!A-Za-z0-9]*")
Last edited by drdoug; January 28th, 2005 at 01:25 PM.
-
January 28th, 2005, 10:54 AM
#6
Re: Is there a way to check if a string is "alphanumeric" ?
Use a regular expression.
See Regular Expressions
-
January 28th, 2005, 01:22 PM
#7
Re: Is there a way to check if a string is "alphanumeric" ?
There should be a notice in huge red font displayed all day that says
YOU ARE ABOUT TO RESPOND TO A THREAD THAT HAS BEEN INACTIVE FOR 4 YEARS
Last edited by Andreas Masur; January 29th, 2005 at 03:47 AM.
"The Chicken and Rice MRE is not a personal lubricant."
-
January 28th, 2005, 07:01 PM
#8
Re: Is there a way to check if a string is "alphanumeric" ?
w3rd
drdoug; dont make a habit of this. dragging up a 4 year old JAVA thread only to go and post some VISUAL BASIC into it is very.. well, im sure you can imagine
-
January 29th, 2005, 04:01 PM
#9
Re: Is there a way to check if a string is "alphanumeric" ?
LOL....By the way - as I cannot refrain myself - how about using
regular expressions? They should be quite a common thing in Java,
are not ?
AnotherVbMadPosterWithABrokenClock
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
-
January 29th, 2005, 04:24 PM
#10
Re: Is there a way to check if a string is "alphanumeric" ?
I don't really see what the problem is, String has a matches(...) method that will tell you if it matches a regular expression:
Code:
if (myString.matches("[a-zA-z0-9]*")) {
System.out.println("Alphanumeric");
}
else {
System.out.println("Not alphanumeric");
}
Think (design) globally; act (code) locally...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
January 30th, 2005, 07:21 PM
#11
Re: Is there a way to check if a string is "alphanumeric" ?
indeed, except, at the time the original post was made, String.matches() did not exist.
additionally, matches() is slower than a simple array search:
Code:
//pseudocode
char[] c = myString.toCharArray();
for(each charC in c){
if(charC < '0' || charC > '9')
break and declare not numeric
}
declare numeric
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|