我有一个从列表 A 中的查询返回的字符串列表。
我正在尝试使用 String Utils.join 将列表中的值组合为以逗号和引号分隔的字符串。但它没有按预期工作。
abcList 中的值 - [abc、cde、fgh]
abcList.addAll(jdbcTemplate.queryForList(abcSql, String.class));
String abc= StringUtils.join(abcList, "','");
abc = "'" +abc+ "'";
预期输出 - 'abc'、'cde'、'fgh'
实际输出 - 'abc, cde, fgh'
我不确定我在这里做错了什么,因为我想将字符串 abc 中的值传递到带有“IN”条件的查询中。
最佳答案
作为替代方案,您还可以使用stream.Collectors.joining
List<String> myList = Arrays.asList("abc","def","ghi");
String joined = myList.stream().collect(Collectors.joining("','", "'", "'"));
System.out.println(joined);
关于java - 字符串实用程序加入未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49410032/