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
  • 3,473
  • 17
  • 65
  • 143

2 Answers2

4

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
  • 13,003
  • 2
  • 20
  • 25
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
  • 131,952
  • 11
  • 151
  • 228