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 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短