Java String join() Method with Examples
The join() method requires two inputs.
Elements: the items that need to be linked together delimiter – the delimiter Any class that implements CharSequence may be sent to join().
If one is passed, the elements of an iterable will be combined. The iterable must implement CharSequence. Because these classes implement CharSequence, they are CharSequence.
When the static String concatenates each string, a delimiter is placed between them. The join() method, which is part of the String class, creates a single string by joining the provided string and the delimiter.
Syntax:
Both public static String join (CharSequence deli, Iterable? extends CharSequence> ele) and public static String join (CharSequence deli, CharSequence… ele) are available.
Parameters:
Each element must have a delimiter associated with it. The delimiter may be a string or character.
Returns a joined string with a delimiter.
Example:
class Techvidvan{
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);
}
}
For instance, omitting the StringJoiner class and the String.join() function Approach:
We won’t be using Java 8 in this program; instead, we’ll attempt to complete it manually and see how much larger a code base or number of steps were required to accomplish our objective.
First, we will unite the three strings “DSA,” “FAANG,” and “ALGO,” using the delimiter “gfg.” To do this, we must write the delimiter each time before a new string is added.
We will next unite the components of an ArrayList containing the strings “DSA,” “FAANG,” and “ALGO” using the delimiter “gig. ” To do this, we had to iterate over the ArrayList.
Notes:
Elements: A char value with a delimiter must be appended, and a combined string with a delimiter is returned.
If the element or delimiter is null, the exception throws a NullPointerException.
Example:
// 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);
}
}
public class StringJoinExample3
{
// main method
public static void main(String argvs[])
{
String str = null;
str = String.join(null, "abc", "bcd", "apple");
System.out.println(str);
}
}
Output:
Exception in thread “main” java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:221)
at java.base/java.lang.String.join(String.java:2393)
at StringJoinExample3.main(StringJoinExample3.java:7)
using System;
using System.Collections.Generic;
Example:
public class Example
{
public static void Main()
{
int maxPrime = 100;
List<int> primes = GetPrimes(maxPrime);
Console.WriteLine("Primes less than {0}:", maxPrime);
Console.WriteLine(" {0}", String.Join(" ", primes));
}
private static List<int> GetPrimes(int maxPrime)
{
Array values = Array.CreateInstance(typeof(int),
new int[] { maxPrime - 1}, new int[] { 2 });
// Use Sieve of Eratosthenes to determine prime numbers.
for (int ctr = values.GetLowerBound(0); ctr <= (int) Math.Ceiling(Math.Sqrt(values.GetUpperBound(0))); ctr++)
{
if ((int) values.GetValue(ctr) == 1) continue;
for (int multiplier = ctr; multiplier <= maxPrime / 2; multiplier++)
if (ctr * multiplier <= maxPrime)
values.SetValue(1, ctr * multiplier);
}
List<int> primes = new List<int>();
for (int ctr = values.GetLowerBound(0); ctr <= values.GetUpperBound(0); ctr++)
if ((int) values.GetValue(ctr) == 0)
primes.Add(ctr);
return primes;
}
}
// The example displays the following output:
// Primes less than 100:
// 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
NOTES:
String is returned.
A string made up of value items separated by a separator string.
-or-
If values contain no elements, it is empty.
Exceptions
The values of ArgumentNullException are null.
OutOfMemoryException
The generated string’s length exceeds the maximum allowable length (Int32.MaxValue).
Conclusion
The java string join() method connects all strings with a delimiter specified by the user. Because this method is a static method of the Java String class, we don’t need to construct any objects to use it.

