CodeGym /Java Blog /Strings in Java /Java String join() method
Author
Milan Vucic
Programming Tutor at Codementor.io

Java String join() method

Published in the Strings in Java group

What is Java String Join() method?

The java string join() method joins all the strings with a delimiter provided to this method. We don’t need to create any object for using this method because it is a static method of the Java String class.

Forms of Join method

There are two forms of join methods in the Java String class or you can say it has two overloaded forms. One joins the number of strings provided as parameters and the other joins the array or list of strings by iterating them but the number of strings or the elements of the array or list must implement CharSequence Interface. Let’s discuss them in detail with examples.

Join Strings

This method takes a delimiter as a first parameter and then we can provide it with one or many charsequence elements or strings to be concatenated.

Syntax


//CharSequence... represents that more than one element can be passed
public static String join(CharSequence delimiter, CharSequence... elements)

Parameters

  1. Deleimeter works as a separator among the elements.
  2. Elements that needed to be joined.

Returns

It always returns a concatenated string of elements joined by the delimiter.

Example


class Main {
  public static void main(String[] args) {
    String str1 = "Concatenating";
    String str2 = "the";
    String str3 = "strings";
    String str4 = "with";
    String str5 = "space";
    String str6 = "as";
    String str7 = "delimiter";

    // joining strings with space between them as delimiter
    String joinedStr = String.join(" ", str1, str2, str3, str4, str5, str6, str7);

    System.out.println(joinedStr);
  }
}

Output

Concatenating the strings with space as delimiter

Join List

This method takes a delimiter as a first parameter and an iterable as a second parameter whose elements must implement CharSequence and will be joined.

Syntax


public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

Parameters

  1. Deleimeter works as a separator among the elements.
  2. Iterable which can be a list or array of elements, needs to be joined.

Returns

It always returns a concatenated string of elements joined by the delimiter.

Example


import java.util.ArrayList;

class Main {
  public static void main(String[] args) {
    ArrayList<String> arrayOfStrings = new ArrayList<>();
    arrayOfStrings.add("Concatenating");
    arrayOfStrings.add("the");
    arrayOfStrings.add("array");
    arrayOfStrings.add("list");
    arrayOfStrings.add("elements");
    arrayOfStrings.add("with");
    arrayOfStrings.add("space");
    arrayOfStrings.add("as");
    arrayOfStrings.add("delimiter");

    // joining arrayList with space between them as delimiter
    String joinedStr = String.join(" ", arrayOfStrings);

    System.out.println(joinedStr);
  }
}

Output

Concatenating the array list elements with space as delimiter
Java String join() method - 1

Java String join() Example


class Main {
  public static void main(String[] args) {
    String str1 = "2022";
    String str2 = "9";
    String str3 = "12";

    // joining strings with - between them as delimiter to show date
    String joinedStr1 = String.join("-", str1, str2, str3);

    System.out.println(joinedStr1);
    
    // joining strings with / between them as delimiter to show date
    String joinedStr2 = String.join("/", str1, str2, str3);

    System.out.println(joinedStr2);
  }
}

Output

2022-9-12 2022/9/12

Conclusion

We hope by now you understand what java string join() method is and how to implement it’s both forms. Feel free to practice and get back whenever you need more assistance. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION