Java Program to Merge Two Strings Constructed Using Stringjoiner Class

You are currently viewing Java Program to Merge Two Strings Constructed Using Stringjoiner Class

Java program to merge two strings Constructed using StringJoiner class

// Java program to merge two strings Constructed 
// using StringJoiner class

import java.util.*;

public class Main {
  public static void main(String[] args) {
    StringJoiner str1 = new StringJoiner(",");
    StringJoiner str2 = new StringJoiner(":");

    str1.add("ABC");
    str1.add("LMN");
    str1.add("PQR");
    str1.add("XYZ");

    str2.add("111");
    str2.add("222");
    str2.add("333");
    str2.add("444");

    str1.merge(str2);

    System.out.println("Constructed String is: '" + str1 + "'");
  }
}

Output

Constructed String is: 'ABC,LMN,PQR,XYZ,111:222:333:444'

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply