0

I have array of Strings for example:

String[] arr = {"one", "two", "three"};

Is possible with Guava Joiner get string like this:

"<one>, <two>, <three>"

where , is separator and < > are prefix and suffix for every element. Thanks.

Denis Stephanov
  • 4,563
  • 24
  • 78
  • 174

2 Answers2

5

You can use also Collectors.joining() like below:

    String[] arr = {"one", "two", "three"};        
    String joined = Stream.of(arr).collect(Collectors.joining(">, <", "<", ">"));
    System.out.println(joined);
Eritrean
  • 15,851
  • 3
  • 22
  • 28
1

Use a Joiner with the end of one and the start of the next:

Joiner.on(">, <")

And then just put a < on the start, and > on the end.

"<" + Joiner.on(">, <").join(arr) + ">"

You might want to handle the empty array case, to distinguish this from {""}:

(arr.length > 0) ? ("<" + Joiner.on(">, <").join(arr) + ">") : ""
Andy Turner
  • 137,514
  • 11
  • 162
  • 243