1

I have tried this code. Its not working properly..

String myString = "343DFDFD"; // "FDFS343434"
System.out.println(myString.matches("[A-Za-z0-9]+")); //  false coming

note: i want output for above alphanumeric is true

Shabbir Dhangot
  • 8,642
  • 9
  • 58
  • 78
harikrishnan
  • 2,135
  • 3
  • 28
  • 63
  • Possible duplicate of [Fastest way to check a string is alphanumeric in Java](http://stackoverflow.com/questions/12831719/fastest-way-to-check-a-string-is-alphanumeric-in-java) – Aman Grover Sep 16 '16 at 12:40
  • this is a true statement . what are u looking for ? – DKV Sep 16 '16 at 12:44

2 Answers2

0

Modify the regex as:

String myString = "343DFDFD"; // "FDFS343434"
System.out.println(myString.matches("^.*[^a-zA-Z0-9 ].*$")); //  false coming
kgandroid
  • 5,459
  • 5
  • 37
  • 68
0
Use it:

public boolean isAlphanumeric(String str) {
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            if (!Character.isDigit(c) && !Character.isLetter(c))
                return false;
        }

        return true;
    }
Rashpal Singh
  • 623
  • 3
  • 13