user1874239
user1874239

Reputation: 325

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?"

Upvotes: 18

Views: 53679

Answers (7)

dreamcrash
dreamcrash

Reputation: 51393

Like suggested in the comments you can do it using 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:

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

Upvotes: 15

Niraj Sonawane
Niraj Sonawane

Reputation: 11055

Java 8

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

Upvotes: 2

Cuga
Cuga

Reputation: 17904

As of Java 8, this has been added to the standard Java API:

String.join() methods:

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 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. By this, I mean if StringJoiner encounters a null in a List, it won't throw a 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());
}

Upvotes: 33

Lama
Lama

Reputation: 2966

Use StringUtils to solve this.

e.g. Apache Commons Lang offers the join method.

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.

Upvotes: 13

Jobin_vibes
Jobin_vibes

Reputation: 31

this is simple method

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

sample is arraylist

Upvotes: -1

TieDad
TieDad

Reputation: 9889

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. You should use StringBuilder class.

Upvotes: 1

awolfe91
awolfe91

Reputation: 1647

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!

Upvotes: 1

Related Questions