Wednesday, January 21, 2026
HomeLanguagesJavaStringJoiner length() method in Java

StringJoiner length() method in Java

The length() of StringJoiner is used to find out the length of the StringJoiner in characters. It 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. The value should be equivalent to toString().length().
Syntax:  

public int length()

Returns: This method returns the length of the current value of StringJoiner
Below programs illustrate the length() method:
Example 1: To demonstrate length() with delimiter ” “

Java




// Java program to demonstrate
// length() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter " "
        StringJoiner str = new StringJoiner(" ");
 
        // Adding elements in the StringJoiner
        str.add("Geeks");
        str.add("for");
        str.add("Geeks");
 
        // Print the StringJoiner
        System.out.println("StringJoiner: "
                           + str.toString());
 
        // Printing the length of str
        // using length() method
        System.out.println(str.length());
    }
}


Output: 

StringJoiner: Geeks for Geeks
15

 

Example 2: To demonstrate length() with delimiter “, “

Java




// Java program to demonstrate
// length() method of StringJoiner
 
import java.util.StringJoiner;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Creating StringJoiner with delimiter ""
        StringJoiner str = new StringJoiner(", ");
 
        // Adding elements in the StringJoiner
        str.add("Geeks");
        str.add("for");
        str.add("Geeks");
        str.add("A");
        str.add("Computer");
        str.add("Portal");
 
        // Print the StringJoiner
        System.out.println("StringJoiner: "
                           + str.toString());
 
        // Printing the length of str
        // using length() method
        System.out.println(str.length());
    }
}


Output: 

StringJoiner: Geeks, for, Geeks, A, Computer, Portal
38

 

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS