17

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

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
user1874239
  • 305
  • 2
  • 6
  • 10

7 Answers7

32

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());
}
Cuga
  • 17,668
  • 31
  • 111
  • 166
15

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);
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
13

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.

Lama
  • 2,886
  • 6
  • 43
  • 59
2

Java 8

final String concatString= List.stream()
                .collect(Collectors.joining(" "));
shmosel
  • 49,289
  • 6
  • 73
  • 138
Niraj Sonawane
  • 10,225
  • 10
  • 75
  • 104
1

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!

awolfe91
  • 1,627
  • 1
  • 11
  • 11
1

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.

TieDad
  • 9,143
  • 5
  • 32
  • 58
  • thanks for responding. it is assumed that my string is under a certain manageable length. its just for a simple class; nothing too complicated ha – user1874239 Dec 04 '12 at 03:36
-1

this is simple method

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

sample is arraylist