Is there a function for arrays in Java like PHP Join()?

Asked

Viewed 45 times

0

Is there a function in Java that is equal (or similar) to Join PHP? If yes, demonstrate your use.

  • What does this Join do? Join two arrays?

  • 2

    Check if this answers your question: https://stackoverflow.com/questions/1515437/java-function-for-arrays-like-phps-join

  • @diegofm, joins 2 arrays in a string

  • 1

    Also: https://stackoverflow.com/a/26195047/5524514

  • @diegofm, please put your answer to #Velasco accept the answer and so close the topic.

  • @Everson was purposely posting the link as a comment. If you want to reply, feel free :)

Show 1 more comment

1 answer

4


Using Java 8 you can do this very neatly and easily with:

String.join(delimiter, elements);

This works in three ways:

1) Specifying the elements directly

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 for android:

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

Example:

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

Reference:

https://stackoverflow.com/questions/1515437/java-function-for-arrays-like-phps-join

https://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-opposite-of-sp/26195047#26195047

  • 2

    Don’t forget to reference the original link post.

  • Updated!! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.