Java- check if string contains only letters

There are many ways to do this in java. Some of them require a for/while loop and check every character separatelly. As always, we are interested in a clean and elegant snippet, that can be easily altered depanding on the occasion.

1
2
3
4
5
if (!javaw0rld13″.matches([a-zA-Z]*)) {
System.out.println(String contains only letter”);
} else {
System.out.println(“Not only letters”);
}

Not much to explain. The interesting part is the method “matches” and the usage of wildcards.

Cheers,
Vasilis