Java String join example

String's join method can be used to join various String by delimiter. It is introduced in java 8.

For example: We need to join various Strings by ",". You can use join method as
String.join(",","java","blog","tutorials");
// Output
"java,blog,tutorial"
There are two overloaded variants of String's join method:
public static String join(CharSequence delimiter, CharSequence... elements)  
and  
public static String join(CharSequence delimiter, Iterable elements)  

String join example:

package org.arpit.java2blog;

public class StringJoinExample {

 public static void main(String[] args) {
  
  System.out.println(String.join(",","java","blog","tutorials"));
  System.out.println(String.join("-","java","blog","tutorials"));
 }

}

When you run above program, you will get below output:
java,blog,tutorials
java-blog-tutorials

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012