Join array elements with a separator in Java

0 votes

Is there a way to join an array, i.e. perform opposite operation of what split() does. Iterating through an array requires either adding a condition (if this is not the last element, add the separator) or using substring to remove the last separator. 

I'm sure there is a certified, efficient way to do it but not able to find it. Please suggest.

Aug 16, 2018 in Java by v.liyyah
• 1,300 points
2,787 views

1 answer to this question.

0 votes

Using Java 8, you can easily perform the required function:

String.join(delimiter, elements);

This can work in 3 ways:

  • Directly specifying the elements
String arr1 = String.join(",", "a", "b", "c");
  • Using arrays
String[] arr = new String[] { "a", "b", "c" };
String arr2 = String.join(",", array);
  • Using iterables
List<String> list = Arrays.asList(array);
String arr3= String.join(",", list);
answered Aug 16, 2018 by geek.erkami
• 2,680 points

Related Questions In Java

0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,140 points
1,543 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,920 points
13,192 views
+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,190 points
1,137 views
0 votes
2 answers

How to left pad a string with zero in java?

String paddedString = org.apache.commons.lang.StringUtils.leftPad("129018", 10, "0") the second ...READ MORE

answered Aug 31, 2018 in Java by Sushmita
• 6,920 points
3,181 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 81,222 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,357 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,432 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,875 views
+2 votes
3 answers

Listing all the subclasses of a specific class in Java

Try with BurningWave core library. Here an ...READ MORE

answered Dec 29, 2019 in Java by anonymous
• 460 points

edited Jan 9, 2020 by anonymous 14,501 views
0 votes
1 answer

Executing commands with parameters in Java

Try using the below command: Runtime.getRuntime().exec(new String[]{"php","/var/www/script.php", "-m", ...READ MORE

answered Sep 5, 2018 in Java by geek.erkami
• 2,680 points
3,126 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP