Java String stripTrailing() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 11+

Definition

In Java, stripTrailing() returns a string with all white space removed from the end, while the beginning and the middle stay exactly as they were. White space follows Character.isWhitespace.

It is the "right trim" counterpart to stripLeading(). Typical uses are padded columns from fixed-width files and lines that end with stray spaces or a line terminator.

stripTrailing() is an instance method of java.lang.String, added in Java 11. Call it on a string, as in text.stripTrailing(); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 11 or newer.

Syntax

Call it with no arguments.

java
String name = row.stripTrailing();
String line = rawLine.stripTrailing();   // also drops \r\n at the end
CallWhat it does
" a ".stripTrailing()" a".
"end\r\n".stripTrailing()"end".
"x".stripTrailing()"x", the same object.

Line terminators are white space too, so stripTrailing() removes a final \n or \r\n along with any spaces before it. Leading indentation is preserved, which matters for code and poetry.

Library declarations
java
public String stripTrailing();

Parameters

ParameterDescription
NonestripTrailing() takes no parameters.

Return Value

Return ValueDescription
StringThe text without trailing white space, or this string if it has none.

Java Examples

Example 1 — Strip Only the Right Side

The same padded text as before, cleaned from the other end.

java
public class StripTrailingBasic {
    public static void main(String[] args) {
        String s = "   left and right   ";

        System.out.println("[" + s.stripTrailing() + "]");
        System.out.println(s.length() + " -> " + s.stripTrailing().length());
    }
}

How It Works

  1. The end is cleaned. The closing bracket now sits right after right.
  2. The start is preserved. The three leading spaces are still there, so indentation survives.
  3. Three chars fewer. The length drops from 20 to 17, exactly the trailing spaces.

Example 2 — Read Names from Fixed-Width Rows

Each record pads its name with spaces to ten characters.

java
public class StripTrailingRows {
    public static void main(String[] args) {
        String[] rows = {"Ada       ", "Grace     ", "Linus     "};

        for (String row : rows) {
            String name = row.stripTrailing();
            System.out.println(name + " (" + name.length() + ")");
        }
    }
}

How It Works

  1. Padded columns. Every row is 10 chars wide, with spaces filling the space after the name.
  2. Drop the padding. stripTrailing() removes those spaces, leaving only the name.
  3. Real lengths. The printed lengths 3, 5, and 5 confirm that no padding remains.

Example 3 — Remove a Line Ending but Keep Inner Spaces

Clean a line read with its Windows line terminator.

java
public class StripTrailingNewline {
    public static void main(String[] args) {
        String line = "last line\r\n";
        String clean = line.stripTrailing();

        System.out.println(line.length() + " -> " + clean.length());
        System.out.println("[" + clean + "]");
        System.out.println("[" + "  keep inner  spaces  ".stripTrailing() + "]");
    }
}

How It Works

  1. Terminators are white space. \r and \n at the end are removed, so 11 chars become 9.
  2. A clean line. The brackets show nothing after line.
  3. Only the tail changes. The leading spaces and the double space inside the last string are kept; only the final two spaces go.

Frequently Asked Questions

It removes white space from the end of the string only and returns a new String.
Java 11.
Yes. \n and \r are white space, so a trailing line terminator is removed with any spaces before it.
Use s.replaceAll("\\s+$", "").
No. Leading white space is preserved. Use stripLeading() or strip() for the start.

Next: subSequence()

Get a slice of the string typed as a CharSequence.

subSequence() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful