gpt4 book ai didi

java - 如何自动添加双引号,将字符串列表转换为逗号分隔值

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:17:07 50 4
gpt4 key购买 nike

假设我有一个字符串列表。

List<String> s = new ArrayList<>();
s.add("one");
s.add("two");
s.add("three");

当我使用 StringUtils.join(",", s) 时,它给我的结果是

"one, two, three"

而我需要输出为

"one","two","three"

我们不喜欢使用 Guava 实用程序,因为该项目未处于 Activity 状态。

是否可以通过 Apache Commons 实用程序实现?

我怎样才能通过实用程序实现这一点,而不是编写自己的逻辑来执行相同的操作?

最佳答案

您可以仅使用 StringUtils 分两步完成,

List<String> s = new ArrayList<>();
s.add("one");
s.add("two");
s.add("three");

String step1 = StringUtils.join(s, "\", \"");// Join with ", "
String step2 = StringUtils.wrap(step1, "\"");// Wrap step1 with "

System.out.println(step2);

输出,

"one", "two", "three"

但是

I need to pass them in a mongo DB query when using $in operator

对于 mongodb 查询,您不需要以这种方式构建它,特别是在 $in 的情况下,您可以通过以下方式查询文档,

BasicDBObject yourInQuery = new BasicDBObject();
yourInQuery.put("in_column", new BasicDBObject("$in", yourList));
DBCursor cursor = collection.find(yourInQuery);

请在以下链接中阅读更多相关信息,

关于java - 如何自动添加双引号,将字符串列表转换为逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39072641/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com