Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

As always, the complete source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.