java的List调用toString以后去除两端[]括号

数据库的vid存的是varchar类型,实体类中定义的是String

java代码:

 			List<Integer> vids=new ArrayList<>();
                if(planList != null && planList.size() > 0){
                    for (int i = 0; i < planList.size(); i++) {
                        VoicePlanListEntity voicePlanListEntity = new VoicePlanListEntity();
                        voicePlanListEntity.setVpid(voicePlanEntity.getId());
                        voicePlanListEntity.setVid(Integer.valueOf(planList.get(i).toString()));
                        voicePlanListEntity.setSort(i);
                        this.voicePlanListService.insert(voicePlanListEntity);

                       vids.add(Integer.valueOf(planList.get(i).toString()));
                    }
                   
                    voicePlanEntity.setVid(vids.toString());
                    voicePlanService.update(voicePlanEntity);
                    //增加语音id
                }

list直接调用toString,最后的结果会有[],如图
在这里插入图片描述
解决方法1、去除[]:

  voicePlanEntity.setVid(vids.toString());

修改为

 voicePlanEntity.setVid(StringUtils.strip(vids.toString(),"[]"));

解决方法二:
用StringBuffer的append方法来拼接字符串,再去除最后一个逗号

StringBuffer sb = new StringBuffer();
for (String str: list) {
sb.append(str).append(",");
}
String keywordStr = sb.deleteCharAt(sb.length() - 1).toString();

我用的第一种方法,成功去除括号[]
在这里插入图片描述


版权声明:本文为weixin_42260782原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。