循环遍历两个字符串以逗号分隔

第一种,单个字符串和多个逗号分隔的

第一步:定义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]);
                }
            }
        }

有更好的方法欢迎评论区留言!!!

    原文作者:大大奇
    原文地址: https://blog.csdn.net/qq_46201721/article/details/111690550
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞