Join Strings in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 06:30:10
Advertisements