Open In App

Java String join() with examples

Last Updated : 04 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The java.lang.string.join() method concatenates the given elements with the delimiter and returns the concatenated string.Note that if an element is null, then null is added.The join() method is included in java string since JDK 1.8.
There are two types of join() methods in java string.
Syntax:

public static String join(CharSequence deli, CharSequence... ele)  
and  
public static String join
(CharSequence deli, Iterable<? extends CharSequence> ele)     
Parameters:
deli- delimiter to be attached with each element 
ele- string or char to be attached with delimiter
Returns :  string joined with delimiter.




// Java program to demonstrate
// working of join() method
  
class Gfg1 {
    public static void main(String args[])
    {
        // delimiter is "<" and elements are "Four", "Five", "Six", "Seven"
        String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven");
  
        System.out.println(gfg1);
    }
}


Output:

Four < Five < Six < Seven




// Java program to demonstrate
// working of join() method
  
class Gfg2 {
    public static void main(String args[])
    {
        // delimiter is "  " and elements are "My",
        // "name", "is", "Niraj", "Pandey"
        String gfg2 = String.join("  ", "My", "name", "is", "Niraj", "Pandey");
  
        System.out.println(gfg2);
    }
}


Output:

My name is Niraj Pandey




// Java program to demonstrate
// working of join() method
  
class Gfg3 {
    public static void main(String args[])
    {
        // delimiter is "->" and elements are "Wake up", 
        // "Eat", "Play", "Sleep", "Wake up"
  
        String gfg3 = String.join("-> ", "Wake up", "Eat",
                      "Play", "Sleep", "Wake up");
  
        System.out.println(gfg3);
    }
}


Output:

Wake up-> Eat-> Play-> Sleep-> Wake up


Similar Reads

StringJoiner Class vs String.join() Method to Join String in Java with Examples
Prior to Java 8 when we need to concatenate a group of strings we need to write that code manually in addition to this we needed to repeatedly use delimiter and sometimes it leads to several mistakes but after Java 8 we can concatenate the strings using StringJoiner class and String.join() method then we can easily achieve our goal. Example: Withou
6 min read
Java Guava | Booleans.join() method with Examples
The join() method of Booleans Class in the Guava library is used to combine or join all the given boolean values separated by a separator. These boolean values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of join operation on the specified boolean valu
2 min read
Java Guava | Shorts.join() method with Examples
Shorts.join() is a method of Shorts class in Guava library which returns a combined string of all the given short values separated by separator. For example, join("-", (short) 1, (short) 2, (short) 3) returns the string "1-2-3". Syntax : public static String join(String separator, short... array) Parameters : separator : The text that should appear
2 min read
Java Guava | Longs.join() method with Examples
The join() method of Longs Class in the Guava library is used to combine or join all the given long values separated by a separator. These long values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of join operation on the specified long values. For exam
2 min read
Java Guava | Doubles.join() method with Examples
The join() method of Doubles Class in the Guava library is used to combine or join all the given double values separated by a separator. These double values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of join operation on the specified double values.
2 min read
Java Guava | Floats.join() method with Examples
The join() method of Floats Class in the Guava library is used to combine or join all the given float values separated by a separator. These float values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of join operation on the specified float values. For
2 min read
Java Guava | Chars.join() method with Examples
The join() method of Chars Class in the Guava library is used to combine or join all the given char values separated by a separator. These char values are passed a parameter to this method. This method also takes the separator as the parameter. This method returns a String which is the result of join operation on the specified char values. For exam
2 min read
Join two ArrayLists in Java
Given two ArrayLists in Java, the task is to join these ArrayLists. Examples: Input: ArrayList1: [Geeks, For, ForGeeks], ArrayList2: [GeeksForGeeks, A computer portal] Output: ArrayList: [Geeks, For, ForGeeks, GeeksForGeeks, A computer portal] Input: ArrayList1: [G, e, e, k, s], ArrayList2: [F, o, r, G, e, e, k, s] Output: ArrayList: [G, e, e, k, s
2 min read
Differences between wait() and join() methods in Java
The wait() and join() methods are used to pause the current thread. The wait() is used in with notify() and notifyAll() methods, but join() is used in Java to wait until one thread finishes its execution. wait() is mainly used for shared resources, a thread notifies other waiting thread when a resource becomes free. On the other hand join() is used
2 min read
Ints join() function | Guava | Java
Guava's Ints.join() returns a string containing the supplied int values separated by separator. For example, join("-", 1, 2, 3) returns the string "1-2-3".Syntax: public static String join(String separator, int[] array) Parameters: This method takes the following parameters: separator: The text that should appear between consecutive values in the r
2 min read