云迈博客

您现在的位置是:首页 > 灌水专栏 > 正文

灌水专栏

JAVA字符串逗号分隔并对每个字符串添加引号

wsinbol2024-03-13灌水专栏44
需求数据库IN查询时,括号内的字符串需要用””包起来实现```Stringstr=“the,quick,brown,,,,,fox,jumped,,,over,the,lazy,dog”;/

需求

数据库IN查询时,括号内的字符串需要用””包起来

实现

String str = "the,quick,brown,,,,,fox,jumped,,,over,the,lazy,dog";
//split the string
List<String> list = Arrays.asList(str.split(",", -1));
// add double quotes around each list item and collect it as a comma separated string
// 核心就是在这一块
String strout = list.stream().collect(Collectors.joining("\",\"", "\"", "\""));
//replace two consecutive double quotes with a empty string
strout = strout.replaceAll("\"\"", "");
System.out.println(strout);

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~