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
- Deleimeter works as a separator among the elements.
- 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
- Deleimeter works as a separator among the elements.
- 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() 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
Practical Applications of String.join()
The String.join()
method simplifies tasks like formatting data for display or constructing file paths. Let's explore some practical scenarios:
Example: Formatting Data for Output
public class DataFormattingExample {
public static void main(String[] args) {
String name = "John Doe";
String age = "30";
String city = "New York";
// Format and display the data
String formattedData = String.join(", ", name, age, city);
System.out.println("Formatted Data: " + formattedData);
}
}
Output: Formatted Data: John Doe, 30, New York
Example: Constructing File Paths
public class FilePathExample {
public static void main(String[] args) {
String root = "C:";
String folder = "Users";
String subfolder = "Documents";
String file = "example.txt";
// Construct the file path
String filePath = String.join("\\", root, folder, subfolder, file);
System.out.println("File Path: " + filePath);
}
}
Output: File Path: C:\Users\Documents\example.txt
Using String.join() in Data Processing Tasks
The String.join()
method is highly useful in data processing scenarios, such as creating CSV strings from data arrays. Here's an example:
Example: Creating a CSV String
import java.util.Arrays;
public class CSVExample {
public static void main(String[] args) {
String[] data = {"John Doe", "30", "New York"};
// Create a CSV string
String csv = String.join(",", data);
System.out.println("CSV Output: " + csv);
}
}
Output: CSV Output: John Doe,30,New York
Constructing Complex Strings with String.join()
The flexibility of String.join()
makes it ideal for constructing complex strings like URLs or command-line commands.
Example: Building a URL
public class URLConstructionExample {
public static void main(String[] args) {
String protocol = "https";
String domain = "example.com";
String path = "search";
String query = "q=java";
// Construct the URL
String url = String.join("/", protocol + ":", "", domain, path) + "?" + query;
System.out.println("URL: " + url);
}
}
Output: URL: https://example.com/search?q=java
Example: Creating Command-Line Commands
public class CommandLineExample {
public static void main(String[] args) {
String command = "ping";
String option = "-n";
String count = "4";
String target = "google.com";
// Construct the command
String fullCommand = String.join(" ", command, option, count, target);
System.out.println("Command: " + fullCommand);
}
}
Output: Command: ping -n 4 google.com
GO TO FULL VERSION