是否有实用方法可以按给定字符串分隔列表?

发布于 2024-08-24 04:58:02 字数 475 浏览 10 评论 0原文

Apache Common Lang 或 Spring Utils 中是否有类似以下内容的内容,或者您​​是否为此编写自己的 Util 方法?

List<String> list = new ArrayList<String>();
list.add("moo");
list.add("foo");
list.add("bar");

String enumeratedList = Util.enumerate(list, ", ");

assert enumeratedList == "moo, foo, bar";

我记得在php中使用implode,这就是我搜索java的内容。

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

Is there something like the following in Apache Common Lang or Spring Utils or do you write your own Util method for this?

List<String> list = new ArrayList<String>();
list.add("moo");
list.add("foo");
list.add("bar");

String enumeratedList = Util.enumerate(list, ", ");

assert enumeratedList == "moo, foo, bar";

I remember the use of implode in php, this is what i search for java.

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞陪衬 2024-08-31 04:58:02

您可以使用 StringUtils.join(Object[] array, String delimiter) (来自 commons-lang)如下方式:

String enumeratedList = StringUtils.join(list.toArray(), ", ");

You can use StringUtils.join(Object[] array, String delimiter) (from commons-lang) in the following way:

String enumeratedList = StringUtils.join(list.toArray(), ", ");
余罪 2024-08-31 04:58:02

Google 收藏集提供 Joiner 类,可以这样使用:

public class Main {

    public static void main(String[] args) {
        List<String> list = Lists.newLinkedList();
        list.add("1");
        list.add("2");
        list.add("3");
        System.out.println(Joiner.on(", ").join(list));
    }

}

Google Collections provides the Joiner class, which can be used like this:

public class Main {

    public static void main(String[] args) {
        List<String> list = Lists.newLinkedList();
        list.add("1");
        list.add("2");
        list.add("3");
        System.out.println(Joiner.on(", ").join(list));
    }

}
半山落雨半山空 2024-08-31 04:58:02

如果您不想依赖 commons-lang,那么实现起来非常简单。将列表转换为数组只是为了再次将其连接为字符串也不是很好。相反,只需迭代您的集合即可。比使用 Collection 更好的是使用 Iterable,它可以处理任何可以迭代的东西(甚至是某种长度未知的流或集合)。

import java.util.Arrays;
import java.util.Iterator;

public class JoinDemo {
  public static String join(String sep, Iterable<String> i) {
    StringBuilder sb = new StringBuilder();
    for (Iterator<String> it = i.iterator(); it.hasNext();) {
      sb.append(it.next());
      if (it.hasNext())
        sb.append(sep);
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    System.out.println(join(",", Arrays.asList(args)));
  }
}

例子:

# javac JoinDemo.java
# java JoinDemo a b c
a,b,c

It's pretty trivial to inplement if you don't want a dependency on commons-lang. It's also not great to convert a List to an Array simply to join it again into a String. Instead just iterate over your collection. Even better than using Collection is using Iterable which handles anything which can be iterator over (even some sort of stream or Collection of unknown length).

import java.util.Arrays;
import java.util.Iterator;

public class JoinDemo {
  public static String join(String sep, Iterable<String> i) {
    StringBuilder sb = new StringBuilder();
    for (Iterator<String> it = i.iterator(); it.hasNext();) {
      sb.append(it.next());
      if (it.hasNext())
        sb.append(sep);
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    System.out.println(join(",", Arrays.asList(args)));
  }
}

Example:

# javac JoinDemo.java
# java JoinDemo a b c
a,b,c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文