check if string contains only alpha numeric characters

A

angelochen960

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
 
A

Arne Vajhøj

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
 
S

Stefan Ram

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
 
A

angelochen960

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

this works, thanks.
 
R

Roedy Green

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

Daniele Futtorovic

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top