两个string类型,string之间按逗号分割,先转换为list再进行去重/不去重操作,直接操作list集合可以自行参考修改
public static void main(String[] args) {
String str1="11,22,44,55,22,11";
String str2="33,11,22";
System.out.println(removeNoDistinct(str1,str2));
}
//移除指定字符串不去重/去重
public static String removeNoDistinct(String str1,String str2) {
List<String> list1 = Arrays.asList(str1.split(","));
List<String> arrList = new ArrayList<String>(list1);
for (int i = 0; i <str2.split(",").length; i++) {
for (int j = 0; j < str1.split(",").length; j++) {
if (str1.split(",")[j].equals(str2.split(",")[i])) {
arrList.remove(str2.split(",")[i]);
//加上break为去重;注释break不去重
break;
}
}
}
return Arrays.toString(arrList.toArray());
}
运行结果不去重
去重