Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

String manipulation is a common task in Java programming. Sometimes, trailing whitespace can lead to inconsistencies, increase storage sizes, and even cause unintended bugs.

In this quick tutorial, we’ll explore effective techniques to remove only trailing spaces or whitespace characters from a given String in Java.

2. Introduction to the Problem

Let’s first create an input String as our example:

final static String INPUT = "  a b c d e \t  ";

When we talk about removing the trailing whitespace characters, the trim() method may come up as the first idea. However, the trim() method removes both leading and trailing whitespace characters:

String result = INPUT.trim();
assertEquals("a b c d e", result);

We must keep the leading whitespace characters in the original input. Therefore, trim() doesn’t solve our problem.

Depending on the requirement, we might need to eliminate all trailing whitespace or exclusively trailing space characters. It’s important to recognize that whitespace includes space and other characters such as TAB (‘\t’).

For instance, considering INPUT as an example, removing its trailing whitespace would yield ” a b c d e”. Conversely, if we intend to remove only trailing space characters from INPUT, we would expect to obtain ” a b c d e \t”. As we can see, the TAB character remains intact.

In this tutorial, we’ll cover these two scenarios and address different solutions to remove trailing spaces or whitespace.

Next, let’s dive into coding.

3. Using Regex and the replaceAll() Method

Regex proves to be a powerful utility for manipulating Strings in Java. By crafting a Regex pattern to match trailing spaces or whitespace, we can effectively utilize the replaceAll() method to address the issue.

For example, since the pattern ” +$” matches trailing consecutive space characters, we can pass this Regex pattern to replaceAll() to remove trailing spaces:

String result1 = INPUT.replaceAll(" +$", "");
assertEquals("  a b c d e \t", result1);

As we can see, the TAB character and the spaces before it remain intact.

Similarly, “\\s+$” is a pattern that removes all trailing whitespace:

String result2 = INPUT.replaceAll("\\s+$", "");
assertEquals("  a b c d e", result2); 

4. Using the stripTrailing() Method

In Java 11, the stripTrailing() method was introduced as a new addition to the String class. As its name implies, stripTrailing() allows us to remove trailing whitespace conveniently:

String result = INPUT.stripTrailing();
assertEquals("  a b c d e", result);

5. Using Apache Commons Lang 3’s stripEnd()

Apache Commons Lang 3 is a widely used library. Its StringUtils class provides a rich set of handy utilities for String manipulations. For example, the stripEnd() method eliminates specified trailing characters from the input String. Therefore, we can use it to remove trailing spaces:

String result = StringUtils.stripEnd(INPUT, " ");
assertEquals("  a b c d e \t", result);

It’s worth mentioning that since stripEnd() only takes a predefined String as the stripChars parameter, we cannot use this method to remove trailing whitespace characters.

6. Conclusion

In this article, we explored different approaches to remove trailing spaces or whitespace. These solutions encompass both the Java standard library and an external library, Apache Commons Lang 3.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW