简体   繁体   English

字符串的 ArrayList 到单个字符串

[英]ArrayList of Strings to one single string

I have an array list of strings (each individual element in the array list is just a word with no white space) and I want to take each element and append each next word to the end of a string.我有一个字符串数组列表(数组列表中的每个单独元素只是一个没有空格的单词),我想获取每个元素并将每个下一个单词附加到字符串的末尾。

So say the array list has所以说数组列表有

    element 0 = "hello"
    element 1 = "world,"
    element 2 = "how"
    element 3 = "are"
    element 4 = "you?"

I want to make a string called sentence that contains "hello world, how are you?"我想创建一个名为句子的字符串,其中包含“hello world,你好吗?”

As of Java 8, this has been added to the standard Java API:从 Java 8 开始,这已添加到标准 Java API 中:

String.join() methods: String.join()方法:

String joined = String.join("/", "2014", "10", "28" ); // "2014/10/28"

List<String> list = Arrays.asList("foo", "bar", "baz");
joined = String.join(";", list); // "foo;bar;baz"

StringJoiner is also added:还添加了StringJoiner

StringJoiner joiner = new StringJoiner(",");
joiner.add("foo");
joiner.add("bar");
joiner.add("baz");
String joined = joiner.toString(); // "foo,bar,baz"

Plus, it's nullsafe, which I appreciate.另外,它是 nullsafe,我很欣赏。 By this, I mean if StringJoiner encounters a null in a List , it won't throw a NPE:我的意思是,如果StringJoinerList遇到null ,它不会抛出 NPE:

@Test
public void showNullInStringJoiner() {
    StringJoiner joinedErrors = new StringJoiner("|");
    List<String> errorList = Arrays.asList("asdf", "bdfs", null, "das");
    for (String desc : errorList) {
        joinedErrors.add(desc);
    }

    assertEquals("asdf|bdfs|null|das", joinedErrors.toString());
}

Like suggested in the comments you can do it using StringBuilder :就像评论中建议的那样,您可以使用StringBuilder来做到这一点:

StringBuilder listString = new StringBuilder();

for (String s : list)
     listString.append(s).append(" ");

or without the explicit loop:或没有显式循环:

list.forEach(s -> listString.append(s).append(" "));

or even more elegant with Java 8 capabilities:甚至更优雅的 Java 8 功能:

String listString = String.join(" ", list);

Use StringUtils to solve this.使用 StringUtils 来解决这个问题。

eg Apache Commons Lang offers the join method.例如,Apache Commons Lang 提供了连接方法。

StringUtils.join(myList,","))

It will iterate through your array or list of strings and will join them, using the 2nd parameter as seperator.它将遍历您的数组或字符串列表,并使用第二个参数作为分隔符加入它们。 Just remember - there is always a library for everything to make things easy.请记住 - 总有一个图书馆可以让一切变得简单。

Java 8爪哇 8

final String concatString= List.stream()
                .collect(Collectors.joining(" "));

Well, a standard for loop should work:好吧,循环的标准应该有效:

String toPrint = "";
for(int i=0; i<list.size(); i++){
  toPrint += list.get(i)+" ";
}
System.out.println(toPrint);

Hope that helps!希望有帮助!

Simplest way:最简单的方法:

String ret = "";
for (int i = 0; i < array.size(); i++) {
    ret += array.get(i) + " ";
}

But if your array is long, performance of string concat is poor.但是如果你的数组很长,字符串 concat 的性能就会很差。 You should use StringBuilder class.您应该使用 StringBuilder 类。

this is simple method这是简单的方法

String value = TextUtils.join(" ", sample); String value = TextUtils.join(" ", sample);

sample is arraylist样本是数组列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM