Remove Only Trailing Spaces or Whitespace From a String in Java – 用 Java 从字符串中删除尾部空格或空白

最后修改: 2024年 3月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

String 操作是 Java 编程中的一项常见任务。有时,尾部空白会导致不一致、增加存储空间,甚至造成意想不到的错误。

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

在本快速教程中,我们将探讨在 Java 中仅删除给定 String 中的尾部空格或空白字符的有效技术。

2. Introduction to the Problem

2.问题介绍

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

让我们首先创建一个输入 String 作为示例:

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:

当我们谈到移除尾部空白字符时,trim() 方法可能是第一个想法。但是,trim() 方法同时删除前导和尾部空白字符

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.

我们必须保留原始输入中的前导空白字符。因此,trim() 并不能解决我们的问题

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’).

根据要求,我们可能需要消除所有尾部空白或只消除尾部空格字符。必须认识到,空白包括空格和其他字符,如 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.

例如,以 INPUT 为例,如果删除其尾部空格,就会得到 ” a b c d e” 。相反,如果我们只打算删除 INPUT 的尾部空格字符, 我们将得到 ” a b c d e\t” 。我们可以看到,TAB 字符保持不变。

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

3.使用 Regex 和 replaceAll() 方法

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.

Regex 被证明是在 Java 中操作 Strings 的强大工具。通过精心设计 Regex 模式来匹配尾部空格或空白,我们可以有效地利用 replaceAll() 方法来解决这个问题。

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

例如,由于 模式 ” +$” 匹配尾部连续空格字符,我们可以将此 Regex 模式传递给 replaceAll() 以删除尾部空格:

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.

我们可以看到,TAB 字符及其前面的空格保持不变。

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

同样,“\s+$” 是一种删除所有尾部空白的模式

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

4. Using the stripTrailing() Method

4.使用 stripTrailing() 方法

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:

在 Java 11 中,String 类新增了 stripTrailing() 方法。顾名思义,stripTrailing() 允许我们方便地删除尾部空白

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

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

5.使用 Apache Commons Lang 3 的 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:

Apache Commons Lang 3 是一个广泛使用的库。它的 StringUtils 类为 String 操作提供了丰富的实用工具。例如,stripEnd() 方法会删除输入String中指定的尾部字符。因此,我们可以使用它来删除尾部空格:

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.

值得一提的是,由于 stripEnd() 仅使用预定义的 String 作为 stripChars 参数,因此我们无法使用此方法删除尾部空白字符

6. Conclusion

6.结论

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.

在本文中,我们探讨了删除尾部空格或空白的不同方法。这些解决方案包括 Java 标准库和外部库 Apache Commons Lang 3。

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

与往常一样,这些示例的完整源代码可在 GitHub 上获取。