Let's assume there is an array:
String[] myArray = new String[]{"slim cat", "fat cat", "extremely fat cat"};
Now I want to transform this array into a String joined with "&"
.
So that the output becomes:
slim cat&fat cat&extremely fat cat
I am trying to keep my code shorter and clean, without the need to manually type a for
loop.
I would like to have a short solution, like we use in reverse way with someString.split();
.
How can I achieve this?
Using Java 8, String.join(CharSequence delimiter, CharSequence... elements)
:
String result = String.join("&", myArray);
Using Java 7 or earlier, you either need a loop or recursion.