Is there a way to check if a string is "alphanumeric" ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Is there a way to check if a string is "alphanumeric" ?

  1. #1
    Join Date
    Aug 1999
    Posts
    89

    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,



  2. #2
    Join Date
    May 2001
    Posts
    594

    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
    Bayard
    bayard@generationjava.com
    http://www.apache.org/~bayard
    http://www.generationjava.com

    Brainbench MVP for Java
    http://www.brainbench.com

  3. #3
    Join Date
    Jul 1999
    Location
    CA, USA
    Posts
    104

    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


  4. #4
    Join Date
    May 2001
    Posts
    594

    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
    Bayard
    bayard@generationjava.com
    http://www.apache.org/~bayard
    http://www.generationjava.com

    Brainbench MVP for Java
    http://www.brainbench.com

  5. #5
    Join Date
    Jan 2005
    Posts
    1

    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.

  6. #6
    Join Date
    Sep 2004
    Posts
    247

    Re: Is there a way to check if a string is "alphanumeric" ?

    Use a regular expression.

    See Regular Expressions

  7. #7
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776

    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."

  8. #8
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    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.

  10. #10
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  11. #11
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    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
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center