0

I would like to validate the String and to ensure that the String contains only alphabets.

Here is my program and else part is getting printed

String myString = "hellothisisastring";
        String reg="[a-zA-Z]";

    if(myString.matches(reg)) 
        { 
        System.out.println("Yep!"); 
        } 
        else 
        { 
    System.out.println("Nope!"); 
        }
Mosam Mehta
  • 1,658
  • 6
  • 25
  • 34
J Moses
  • 9
  • 2

1 Answers1

1

Try to change your regex like

String reg="[a-zA-Z]+";

+ will ensure that it matches string one and more time.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331