java去除字符串中的中括号和双引号,以及还原为去除字符之前的字符串方法,类似于[“1“, “2“]

时间:2025-04-05 07:36:09

程序代码如下

public class tetet {
    public static void main(String[] args) throws JSONException {
        //第一种方式
        String json = "[\"1\", \"2\"]";
        ("去除之前字符串:" + json);
        String t = ("\\\"","");
        ("去除双引号和斜杠后:"+t);
        String replaceAll = ("[", "").replace("]","");
        ("去除左右中括号后:"+replaceAll);

        String [] id = (",");
        List<String> ll = new ArrayList<>();
        for(String s:id){
            s = "\"" + s + "\"";
            (s);
        }
        ("还原字符串:" + ());

        //第二种方式
        String text = "[\"3\", \"4\"]";
        JSONArray jsonArray = (text);
        StringJoiner stringJoiner = new StringJoiner(",");
        for (int i=0;i<();i++){
            ((i))
        }
        ("JSONArray方式:"+());

    	}
}

输出结果如下:

去除之前字符串:["1", "2"]
去除双引号和斜杠后:[1, 2]
去除左右中括号后:1, 2
还原字符串:["1", " 2"]

JSONArray方式:3,4,5