Java String stripTrailing() Method Tutorial

In this section, we will learn what the String stripTrailing() method is and how to use it in Java.

What is Java String stripTrailing() Method?

The Java String stripTrailing() method is used to remove any white space from the trailing part (end) of a string value.

Notes:

  • The method does not change the original value and just returns a new string as a result.
  • If the target string didn’t end with white spaces, the string itself will be returned instead.

Java stripTrailing() Method Syntax:

public String stripTrailing();

stripTrailing() Method Parameters:

The method does not take an argument.

stripTrailing() Method Return Value:

The return value of this method is a new string with its trailing part cleared out of any white space.

stripTrailing() Method Exceptions:

The method does not throw an exception.

Example: using String stripTrailing() method

public class Main {
    public static void main(String[] args) {

        String input = "   hello   ";
        System.out.println("Input string length: " + input.length());
        String stripStr = input.stripTrailing();
        System.out.println("Stripped string length: " + stripStr.length());
    }
}

Output:

Input string length: 11

Stripped string length: 8

 

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies