Is there any function for arrays in Java like PHP's join ()?

0

Is there a function in Java that is the same (or similar) as the join of PHP? If so, demonstrate its use.

    
asked by anonymous 26.05.2017 / 16:53

1 answer

4

Using Java 8 you can do this in a very clean and easy way with:

String.join(delimiter, elements);

This works in three ways:

1) Directly specifying the elements

String joined1 = String.join(",", "a", "b", "c");

2) Using array's

String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);

3) Using iterables

List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);

If you need it for android:

String android.text.TextUtils.join(CharSequence delimiter, Object[] tokens)

Example:

String joined = TextUtils.join(";", MyStringArray);

Reference:

link

    
26.05.2017 / 17:12