时间:2022-09-19 09:27:49 | 栏目:JAVA代码 | 点击:次
如下所示:
public static void main(String[] args) { String str1 = "刘烨,孙坚,王二小,蜘蛛侠,钢铁侠,毛剑卿"; String str2 = "王二小,李占军,刘胡兰,毛剑卿"; String[] arr1 = str1.split(",") ; String[] arr2 = str2.split(",") ; StringBuffer sb = new StringBuffer(); for (int i = 0; i < arr2.length; i++){ for (int j = 0; j < arr1.length; j++){ if (arr1[j].equals(arr2[i])){ sb.append(arr1[j] + ",") ; } } } System.out.println("结果:" + sb.toString().substring(0, sb.toString().length() - 1)); }
输出: "结果:王二小,毛剑卿"