Java String join() Method
What is a string in Java?
In Java, a string object is an object that represents a sequence of characters. A string object is created using the String class, which is a built-in class in Java. Strings are used to represent text data in Java.
- The Java platform provides a rich set of classes and methods for working with strings, making it easy to perform common tasks such as searching, parsing, and formatting strings.
To know more about String Class in java read the complete article.
String Join() method.
The String.join() method is a static method that can be used to concatenate a list of strings into a single string, using a specified delimiter.
- The String.join() method was introduced in Java 8 and is a convenient way to concatenate strings without having to use a loop or manually append the strings together. It can be especially useful when working with large lists of strings.
- The String.join() method is a static method in the java.lang.String class that can be used to join elements of an array or any Iterable object into a single string, using a specified delimiter or separator.
Returns
a new String that is composed of the elements separated by the delimiter
Throws:
NullPointerException - If delimiter or elements is null
Syntax:
public static String join(CharSequence delimiter,CharSequence... elements)
Parameters:
delimiter - the delimiter that separates each element elements - the elements to join together.
Let’s look at a string-related Java program where the Java String Join() Method is used to perform an operation on the given string.
Example: Java String join() Method
public class StringJoinExample { public static void main(String[] args) { String[] words = {"Hello", "World", "Welcome", "to", "Java"}; String sentence = String.join(" ", words); System.out.println(sentence); } }
Output
Hello World Welcome to Java
Explanation:
In this example, the words array is joined together into a single string using a space as the separator. The resulting string is then printed to the console.
Example 2 : Java String join() Method
Run
public class Main { public static void main (String[]args) { String[]fruits = { "apple", "banana", "cherry", "orange", "mango"}; String listOfFruits = String.join (", ", fruits); System.out.println ("List of Fruits: " + listOfFruits); } }
Output
List of Fruits: apple, banana, cherry, orange, mango
Explanation:
In this case, a comma and a space are used as the separators to combine the fruits array into a single string. The words "List of Fruits:" and the resulting string are then printed to the console.
Example 3: how to use the join property:
Run
public class Main { public static void main (String[]args) { String date = String.join ("/", "14", "02", "2023"); System.out.print (date); String time = String.join (":", "09", "00", "55"); System.out.println (" " + time); } }
Output
14/02/2023 09:00:55
Explanation:
Using the String.join() method, two strings are combined in this example. By employing the / separator to combine the "14," "02," and "2023" components of the String array, the first string, "date," is produced. The String array's "09", "00," and "55" elements are combined to form the second string, "time," by using the: separator.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment