Check if the String contains any character in the given set of characters in Java
Description
The following code shows how to check if the String contains any character in the given set of characters.
Example
//from w ww .j a v a 2 s. c o m
public class Main {
public static boolean containsAny(String str, char[] searchChars) {
if (str == null || str.length() == 0 || searchChars == null || searchChars.length == 0) {
return false;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j < searchChars.length; j++) {
if (searchChars[j] == ch) {
return true;
}
}
}
return false;
}
public static void main(String[] argv){
System.out.println(containsAny("java2s.com",new char[]{'j'}));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »