Is there a function in Java that is the same (or similar) as the join of PHP? If so, demonstrate its use.
Is there a function in Java that is the same (or similar) as the join of PHP? If so, demonstrate its use.
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: