Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking if a string has special characters [duplicate]

Tags:

java

regex

how would i check a string for special characters such as $%#, and numbers? I only want chars, ex abcd.

the only method i can think of is to make an arraylist of the alphabet and check each index to see if it is in the arraylist.

thanks

like image 368
Andres Lamberto Avatar asked Dec 21 '22 14:12

Andres Lamberto


1 Answers

RegEx is probably your best bet:

 Pattern p = Pattern.compile("[a-zA-Z]");
 Matcher m = p.matcher(sourceString);
 boolean b = m.matches();
like image 192
lukiffer Avatar answered Feb 15 '23 00:02

lukiffer