Java Utility Library

Java StringJoiner - length() Method



The java.util.StringJoiner.length() method returns the length of the String representation of this StringJoiner. Note that if no add methods have been called, then the length of the String representation (either prefix + suffix or emptyValue) will be returned.

Syntax

public int length()

Parameters

No parameter is required.

Return Value

Returns the length of the current value of StringJoiner.

Exception

NA

Example:

In the example below, the java.util.StringJoiner.length() method is used to find out the length of the given StringJoiner.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a StringJoiner object
    StringJoiner joinNames = new StringJoiner(", ","[","]");
   
    //Adding values to joinNames
    joinNames.add("John");
    joinNames.add("Marry");
    joinNames.add("Kim");

    //printing joinNames 
    System.out.println("joinNames contains: " + joinNames);

    //printing the length of joinNames 
    System.out.print("length of joinNames: ");
    System.out.println(joinNames.length());
  }
}

The output of the above code will be:

joinNames contains: [John, Marry, Kim]
length of joinNames: 18

❮ Java.util - StringJoiner