check if string contains only alpha numeric characters

Discussion in 'Java' started by angelochen960@gmail.com, Aug 19, 2008.

  1. Guest

    hi,

    I need to check if a string contains only alpha numeric characters, no
    space, no other special characters, looks i need a regular expression,
    but too poor in this area, any idea how to do this? thanks.

    Angelo
     
    , Aug 19, 2008
    #1
    1. Advertising

  2. Arne Vajhøj Guest

    wrote:
    > I need to check if a string contains only alpha numeric characters, no
    > space, no other special characters, looks i need a regular expression,


    Try:

    private static Pattern p = Pattern.compile("^[A-Za-z0-9]+$");
    private static boolean match(String s) {
    return p.matcher(s).matches();
    }

    Arne
     
    Arne Vajhøj, Aug 19, 2008
    #2
    1. Advertising

  3. Stefan Ram Guest

    "" <> writes:
    >I need to check if a string contains only alpha numeric characters


    public class Main
    { public static void main( final java.lang.String[] args )
    { java.lang.System.out.println( "abc123".matches( "\\p{Alnum}*" ));
    java.lang.System.out.println( "abc,23".matches( "\\p{Alnum}*" ));
    java.lang.System.out.println( ",bc123".matches( "\\p{Alnum}*" ));
    java.lang.System.out.println( "abc12,".matches( "\\p{Alnum}*" )); }}

    true
    false
    false
    false
     
    Stefan Ram, Aug 19, 2008
    #3
  4. Guest

    On Aug 19, 10:00 am, Arne Vajhøj <> wrote:

    >      private static Pattern p = Pattern.compile("^[A-Za-z0-9]+$");
    >      private static boolean match(String s) {
    >          return p.matcher(s).matches();
    >      }


    this works, thanks.
     
    , Aug 19, 2008
    #4
  5. Roedy Green Guest

    On Mon, 18 Aug 2008 18:47:39 -0700 (PDT), ""
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I need to check if a string contains only alpha numeric characters, no
    >space, no other special characters, looks i need a regular expression,
    >but too poor in this area, any idea how to do this? thanks.


    The following is part of the StringTools class.
    http://mindprod.com/products1.html#COMMON11

    /**
    * Ensure the string contains only legal characters.
    *
    * @param candidate string to test.
    * @param legalChars characters than are legal for candidate.
    * @return true if candidate is formed only of chars from the
    legal set.
    */
    public static boolean isLegal( String candidate, String legalChars
    )
    {
    for ( int i = 0; i < candidate.length(); i++ )
    {
    if ( legalChars.indexOf( candidate.charAt( i ) ) < 0 )
    {
    return false;
    }
    }
    return true;
    }

    ..
    legalChars would be
    "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Aug 19, 2008
    #5
  6. On 19/08/2008 04:00, Arne Vajhøj allegedly wrote:
    > wrote:
    >> I need to check if a string contains only alpha numeric characters, no
    >> space, no other special characters, looks i need a regular expression,

    >
    > Try:
    >
    > private static Pattern p = Pattern.compile("^[A-Za-z0-9]+$");
    > private static boolean match(String s) {
    > return p.matcher(s).matches();
    > }
    >
    > Arne


    <code>
    private static Pattern p = Pattern.compile("[^A-Za-z0-9]");
    private static boolean match(String s) {
    return ! p.matcher(s).find();
    }
    </code>

    Might be quicker.

    -.-

    The least overhead would be with:

    <code>
    public static boolean checkString(String s){
    for(int ii = s.length(); ii --> 0; ){
    if( ! Character.isLetterOrDigit(s.charAt(ii)) )
    return false;
    }

    return true;
    }
    </code>

    Though I18N might be an issue.

    --
    DF.
     
    Daniele Futtorovic, Aug 19, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lala
    Replies:
    1
    Views:
    1,232
    bruce barker
    Nov 17, 2003
  2. Replies:
    5
    Views:
    946
    X-Centric
    Jun 30, 2005
  3. Replies:
    14
    Views:
    712
    Jacek Dziedzic
    Apr 6, 2007
  4. John Smith
    Replies:
    8
    Views:
    175
    David K. Wall
    Sep 26, 2003
  5. .Net Sports
    Replies:
    7
    Views:
    212
    shimmyshack
    Apr 19, 2007
Loading...

Share This Page