Java String join() Method

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.

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

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

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription