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 送芒果的祝福语简短
18 送给学长毕业祝福语简短