×

Java Programs

Java Practice

Java program to get the length of string Constructed by StringJoiner class

Java example to get the length of string Constructed by StringJoiner class.
Submitted by Nidhi, on June 09, 2022

Problem statement

In this program, we will create a string using StringJoiner class with a specified delimiter, and add the substrings using add() method. Then we will find the length of constructed string using the length() method.

Source Code

The source code to get the length of string Constructed by StringJoiner class is given below. The given program is compiled and executed successfully.

// Java program to get the length of string Constructed 
// by StringJoiner class

import java.util.*;

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

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

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

Output

Constructed String is: 'ABC,LMN,PQR,XYZ'
Length of string is: 15

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. After that, we found the length of the string using the length() method and printed the result.

Java StringJoiner Class Programs »





Comments and Discussions!

Load comments ↻





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