Join Strings in Java



To join strings in Java, use the String.join() method. The delimiter set as the first parameter in the method is copied for each element.

Let’s say we want to join the strings “Demo” and “Text”. With that, we want to set a delimeter $. For that, use the join() method as shown below −

String.join("$","Demo","Text");

The following is an example.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String str = String.join("$","Demo","Text");
      System.out.println("Joined strings: "+str);
   }
}

Output

Joined strings: Demo$Text
Updated on: 2020-06-27T06:32:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements