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,954
  • 10
  • 58
  • 80
harikrishnan
  • 1,985
  • 4
  • 32
  • 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,507
  • 5
  • 39
  • 69
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
  • 633
  • 3
  • 13