第一种,单个字符串和多个逗号分隔的
第一步:定义2个字符串
String receiver = "zhangsan,lisi,wangwu";
String receiver1 = "zhangsan";
第二步:定义逗号分隔数组
String[] str = receiver.split(",");
第三步:循环比对
for (int i = 0; i < str.length; i++) {
if (str[i].equals(receiver1)){
System.out.println(receiver1);
}
}
第二种,多个逗号分隔字符串对比
第一步:定义2个字符串
String receiver = "zhangsan,lisi,wangwu";
String receiver1 = "zhangsan,lisi";
第二步:定义逗号分隔数组
String[] str = receiver.split(",");
String[] str1 = receiver.split(",");
for (int i = 0; i < str1.length; i++) {
String[] str2 = receiver1.split(",");
for (int j = 0; j <str2.length ; j++) {
if (str1[i].equals(str2[j])){
System.out.println(str1[i]+"----------"+str2[j]);
}
}
}
有更好的方法欢迎评论区留言!!!