⬅ Previous Topic
Java StringisEmpty()
methodNext Topic ⮕
Java StringlastIndexOf()
methodjoin()
method⬅ Previous Topic
Java StringisEmpty()
methodNext Topic ⮕
Java StringlastIndexOf()
methodString.join()
The String.join()
method in Java is a convenient way to concatenate strings with a specified delimiter. It simplifies what used to require more verbose code, making your string manipulation cleaner and easier to read. Think of it like gluing pieces of string together using a specific connector – that connector is the delimiter.
public static String join(CharSequence delimiter, CharSequence... elements)
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Parameter | Description |
---|---|
delimiter | The sequence of characters to insert between the elements. Can be a String or any other CharSequence. |
elements (variable) | An array of CharSequence objects to join together. Or, an iterable collection of CharSequence objects. |
The join()
method returns a single string that is the result of concatenating all the elements separated by the specified delimiter.
This example demonstrates how to use String.join()
to combine an array of strings into a single string, using a comma and space as the delimiter.
import java.util.*;
public class JoinExample {
public static void main(String[] args) {
String[] words = {"Hello", "World", "Java"};
String joinedString = String.join(", ", words);
System.out.println(joinedString);
}
}
Hello, World, Java
Explanation: We have an array of strings called words
. We call String.join()
with ", " as the delimiter and pass the words
array as the elements to be joined. The result is a new string where each word from the array is separated by a comma and a space.
This example shows how to use String.join()
with an iterable collection, like an ArrayList.
import java.util.*;
public class JoinIterableExample {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
String joinedNames = String.join(" - ", names);
System.out.println(joinedNames);
}
}
Alice - Bob - Charlie
Explanation: Here, we create an ArrayList
of strings called names
. We then use String.join()
to join these names together using " - " as the delimiter. The resulting string combines all the names with a hyphen and space in between.
This example demonstrates what happens when you use an empty string as the delimiter, effectively concatenating the strings directly without any separator.
import java.util.*;
public class JoinEmptyDelimiterExample {
public static void main(String[] args) {
String[] parts = {"a", "b", "c"};
String concatenatedString = String.join("", parts);
System.out.println(concatenatedString);
}
}
abc
Explanation: In this case, we pass an empty string ("") as the delimiter to String.join()
. This means there will be no separator between the elements when they are joined together, resulting in a single concatenated string.
This example shows how String.join()
handles null elements within the array or iterable.
import java.util.*;
public class JoinNullElementsExample {
public static void main(String[] args) {
String[] mixed = {"one", null, "three"};
String joinedWithNulls = String.join(", ", mixed);
System.out.println(joinedWithNulls);
}
}
one, , three
Explanation: When String.join()
encounters a null element, it treats it as an empty string. So, in this example, the null element effectively disappears and is replaced by the delimiter.
This demonstrates using special characters within the delimiter.
import java.util.*;
public class JoinSpecialCharsExample {
public static void main(String[] args) {
String[] words = {"first", "second", "third"};
String joinedWithNewline = String.join("\n", words);
System.out.println(joinedWithNewline);
}
}
first
second
third
Explanation: This example demonstrates joining with a newline character represented by "\n". The output shows each word on a separate line due to the newline delimiter.
⬅ Previous Topic
Java StringisEmpty()
methodNext Topic ⮕
Java StringlastIndexOf()
methodYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.