Skip to content
Related Articles

Related Articles

String Class stripTrailing() Method in Java with Examples
  • Last Updated : 02 Feb, 2021

This method is used to strip trailing whitespaces from the string i.e stripTrailing() method removes all the whitespaces only at the ending of the string. 



String name = "   kapil   ";
System.out.println("#" + name + "#);
System.out.println("#" + name.stripLeading());


#   kapil   #
#   kapil# // trailing whitespaces are removed 


public String stripTrailing()

Parameter: It accepts no parameters.

Returns: The string after removing all the whitespaces at the end of the string.

Note: Like the stripTrailing() method in java, we have strip() (removes leading and trailing whitespace) and stripLeading() (removes the only leading whitespace). 


// Java program to demonstrate the usage of
// stripTrailing() method in comparison to 
// other methods
class GFG {
    public static void main(String[] args)
        // creating a string
        String str = "  Hello,  World ";
        // print the string without any function
        System.out.println("String is");
        System.out.println("#" + str + "#");
        // using strip() method
        System.out.println("Using strip()");
        System.out.println("#" + str.strip() + "#");
        // using stripLeading() method
        System.out.println("Using stripLeading()");
        System.out.println("#" + str.stripLeading() + "#");
        // using stripTrailing() method
        System.out.println("Using stripTrailing()");
        System.out.println("#" + str.stripTrailing() + "#");
String is
#  Hello,  World #

Using strip()
#Hello,  World#

Using stripLeading()
#Hello,  World #

Using stripTrailing()
#  Hello,  World#

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :