Java program to call add() method in a cascaded manner from an object of StringJoiner class

Java example to call add() method in a cascaded manner from an object of StringJoiner class.
Submitted by Nidhi, on June 09, 2022

Problem Solution:

In this program, we will use StringJoiner class to create a string. Here, we will call add() method in a cascaded manner from the object of the StringJoiner class.

Program/Source Code:

The source code to call add() method in a cascaded manner from an object of StringJoiner class is given below. The given program is compiled and executed successfully.

// Java program to call add() method in a cascaded manner 
// from an object of StringJoiner class

import java.util.*;

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

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

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

Output:

Constructed String is: 'ABC,LMN,PQR,XYZ'

Explanation:

In the above program, we imported the "java.util.*" package to use the StringJoiner class. Here, we created a public class Main.

The Main class contains a main() method. The main() method is the entry point for the program. And, created an object of StringJoiner class to construct a string with the specified delimiter ",". Then we added substrings using add() method. Here we called add() method in a cascaded manner. After that, we printed the constructed string.

Java StringJoiner Class Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.