Java程序检查字符串是否在给定的字符集中包含任何字符
在这里,我们有一个字符串。
String str = "abcde";
在上面的字符串中,我们要搜索以下字符集。
//要搜索的字符集
char[] chSearch = {'b', 'c'};为此,请遍历字符串的长度并检查字符串str中的每个字符。如果在字符串中找到“chSearch”中的匹配字符,则将成功。
以下是一个示例。
示例
public class Demo {
public static void main(String[] args) {
String str = "abcde";
//要搜索的字符集
char[] chSearch = {'b', 'c'};
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j < chSearch.length; j++) {
if (chSearch[j] == ch) {
System.out.println("Character "+chSearch[j]+" found in string "+str);
}
}
}
}
}输出结果
Character b found in string abcde Character c found in string abcde
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短