Tuesday, September 6, 2022

Java join() Method - Joining Strings

Though String is a very important class and it provides a lots of methods for comparison of strings, searching with in string but somehow there was no method for joining multiple strings in Java. In Java 8 join() method has been added in the String class which makes it very easy to join the multiple strings. There is also a StringJoiner class in Java 8 that can be used for joining the Strings.

There are many scenarios when you want to join multiple strings to create a new string may be with a delimiter too between two strings, like pipe (|) or comma (,). Before Java 8 you would have used StringBuilder or StringBuffer class in Java to append the string one by one for joining or may be you would have used any third party library like Apache Commons which has StringUtils class.

Code snippet using StringBuilder to join Strings in Java

StringBuilder sb = new StringBuilder();
boolean firstFlg = true;
String delimiter = “,”;
for (String str: strArray){
  if (firstFlg){
    firstFlg = false;
  }
  else{       
    sb.append(delimiter);
  }
  sb.append(str);
}
return
sb.toString();

join() Method in Java String Class

With the addition of join() method in the String class it has become easy to join multiple strings.

There are two overloaded variants of join() method-

  • public static String join(CharSequence delimiter, CharSequence... elements)- Returns a new String composed of copies of the CharSequence elements joined together using the specified delimiter.
  • public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) – Here elements is an Iterable that will have its elements joined together and delimiter is a sequence of characters that is used to separate each of the elements in the resulting String

join() method Java examples

Let's see some examples of joining strings in Java using join() method.

  1. If you have 3 string variables which you want to join with space in between or with '-' in between then it can be done using the following Java code-
    public class StringJoin {
     public static void main(String[] args) {
      String str1 = "An";
      String str2 = "example";
      String str3 = "string";
      
      // joining with space
      String finalStr = String.join(" ", str1, str2, str3);
      System.out.println("str - " + finalStr);
      // joining with hyphen
      finalStr = String.join("-", str1, str2, str3);
      System.out.println("str - " + finalStr);
     }
    }
    

    Output

    str - An example string
    str - An-example-string
    
  2. If you have 3 string variables day, month and year which you want to join to create a date in format dd/mm/yyyy, then using join() method you can do it as follows.
    public class StringJoin {
     public static void main(String[] args) {
      String str1 = "26";
      String str2 = "06";
      String str3 = "2019"; 
      String finalStr = String.join("/", str1, str2, str3);
      System.out.println("str - " + finalStr);  
     }
    }
    

    Output

    str - 26/06/2019
    
  3. If you have list of Strings then you can use the second join method (where Iterable is passed as parameter) to join all the strings with in the list.
    import java.util.ArrayList;
    import java.util.List;
    
    public class ListJoin {
      public static void main(String[] args) {
        List<String> strList = new ArrayList<String>();
        strList.add("An");
        strList.add("example");
        strList.add("string");
        // joining with comma as delimiter
        String finalStr = String.join(",", strList);
        System.out.println("str - " + finalStr);
      }
    }
    

    Output

    str – An,example,string
    
  4. Using join() method to join all the elements of a Set.
    public class JoinSet {
     public static void main(String[] args) {
      Set<String> strings = new LinkedHashSet<>(List.of("An", "example", "string"));
      String joinedStr = String.join(":", strings);
      System.out.println("Joined String- " + joinedStr);
     }
    }
    

    Output

    Joined String- An:example:string
    

That's all for this topic Java join() Method - Joining Strings. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related topics

  1. String in Java Tutorial
  2. String Vs StringBuffer Vs StringBuilder in Java
  3. Java String substring() Method - Getting Substring
  4. How to Create Immutable Class in Java
  5. Java String Interview Questions And Answers

You may also like-

  1. Find All Permutations of a Given String Java Program
  2. Check Given Strings Anagram or Not Java Program
  3. Difference Between Checked And Unchecked Exceptions in Java
  4. static block in Java
  5. this Keyword in Java With Examples
  6. Dependency Injection in Spring Framework
  7. TreeSet in Java With Examples
  8. Java Exchanger With Examples