Basic java questions: String a: 1, 2, 3, 5, 6 String b: 1, 3 Result: 2, 5, 6

/**
     * String 转 List<String>
     * strs:字符串
     * regex:截取符号
     */
    public static List<String> stringToList(String strs, String regex){
    
    
        String str[] = strs.split(regex);
        return Arrays.asList(str);
    }

    /**
     *  List<String> 转 String
     *  lists:字符串集合
     *  regex:拼接符号
     */
    public static String listToString(List<String> lists, String regex) {
    
    
        String join = StringUtils.join(lists, regex);
        return join;
    }

    /**
     * 集合去存
     * all:全部的字符串
     * exist:全部的字符串要去掉的字符串
     */
    public static List<String> remove(List<String> all, List<String> exist) {
    
    
        List<String> copy = new ArrayList<>();
        copy.addAll(all);
        copy.removeAll(exist);
        return copy;
    }
	/**
	 * 字符串a:1,2,3,5,6 
	 * 字符串b:1,3
	 * 结果:2,5,6
	 */
    public static void main(String[] args) {
    
    
        String a = "10,20,30,50,60";
        String b = "10,30";
        List<String> all = Demo.stringToList(a, ",");
        List<String> exist = Demo.stringToList(b, ",");
        List<String> result = Demo.remove(all, exist);
        String s = Demo.listToString(result, ",");
        System.out.println(s);
    }

Guess you like

Origin blog.csdn.net/u010318957/article/details/79894258