Remove Whitespace From a String in Java – 在Java中删除字符串中的空格

最后修改: 2022年 7月 22日

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

1. Overview

1.概述

When we manipulate Strings in Java, we often need to remove whitespace from a String.

当我们在Java中操作Strings时,我们经常需要从String中删除空格。

In this tutorial, we’ll explore common scenarios of removing whitespace from a String in Java.

在本教程中,我们将探讨在Java中从String中去除空白的常见情况。

2. Introduction to the Problem

2.对问题的介绍

To understand the problem easier, let’s first see a string example:

为了更容易理解这个问题,让我们先看一个字符串的例子。

String myString = "   I    am a    wonderful String     !   ";

The example above shows that the myString variable contains multiple leading, trailing spaces, and whitespace characters in the middle.

上面的例子显示,myString变量包含多个前导、尾部空格,以及中间的空白字符。

Usually, when we need to deal with a string like myString in Java, we often face these two requirements:

通常,当我们需要在Java中处理像myString这样的字符串时,我们经常面临这两个要求。

  • removing all whitespace characters from the given string -> “IamawonderfulString!”
  • replacing consecutive whitespace characters with a single space and removing all leading and trailing whitespace characters -> “I am a wonderful String !”

Next, we’ll address two approaches for each case: using the handy replaceAll() method from the String class and the StringUtils class from the widely used Apache Commons Lang3 library.

接下来,我们将针对每种情况讨论两种方法:使用方便的replaceAll()方法从String类和StringUtils类从广泛使用的Apache Commons Lang3>图书馆。

To make it simple, in this tutorial, we don’t cover whitespace characters in the Unicode character set when we talk about whitespace. Further, we’ll use test assertions to verify each solution.

为了简单起见,在本教程中,当我们谈论留白时,我们不涉及Unicode字符集中的留白字符。此外,我们将使用测试断言来验证每个解决方案。

Now, let’s see them in action.

现在,让我们看看他们的行动。

3. Removing All Whitespace From a String

3.删除字符串中的所有空白处

3.1. Using String.replaceAll()

3.1.使用String.replaceAll()

First, let’s remove all whitespace from a string using the replaceAll() method.

首先,让我们使用replaceAll()方法删除一个字符串中的所有空白。

replaceAll() works with regular expressions (regex). We can use the regex character class ‘\s‘ to match a whitespace character. We can replace each whitespace character in the input string with an empty string to solve the problem: inputString.replaceAll(“\\s”, “”).

replaceAll()regular expressions(regex)一起工作。我们可以使用regex字符类’s‘来匹配一个空白字符。我们可以用一个空字符串替换输入字符串中的每个空白字符来解决这个问题:inputString.replaceAll(“”\s”, “”)

Next, let’s create a test to see if this idea works with our example string:

接下来,让我们创建一个测试,看看这个想法对我们的例子字符串是否有效。

String result = myString.replaceAll("\\s", "");
assertThat(result).isEqualTo("IamawonderfulString!");

If we run the test, it passes. So, the replaceAll() method solves the problem. Next, let’s solve the problem using Apache Commons Lang3.

如果我们运行测试,它通过了。所以,replaceAll() 方法解决了这个问题。接下来,让我们用Apache Commons Lang3来解决这个问题。

3.2. Using the Apache Commons Lang3 Library

3.2.使用Apache Commons Lang3库

The Apache Commons Lang3 library ships with a StringUtils utility, which allows us to manipulate strings conveniently.

Apache Commons Lang3库带有一个StringUtils工具,它允许我们方便地操作字符串。

To get started using Apache Commons Lang 3, let’s add the Maven dependency:

要开始使用Apache Commons Lang 3,让我们添加Maven依赖项

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

If we check the methods in the StringUtils class, there’s one method called deleteWhitespace(). The name implies that it’s the method that we’re looking for.

如果我们检查StringUtils类中的方法,有一个方法叫做deleteWhitespace()。这个名字暗示了它就是我们要找的那个方法。

Next, let’s remove all whitespace from a string using StringUtils.deleteWhitespace():

接下来,让我们用StringUtils.deleteWhitespace()从一个字符串中删除所有的空白。

String result = StringUtils.deleteWhitespace(myString);
assertThat(result).isEqualTo("IamawonderfulString!");

The test passes if we execute it. So, the deleteWhitespace() does the job.

如果我们执行它,测试就会通过。所以,deleteWhitespace()完成了工作。

4. Replacing Consecutive Whitespace Characters With One Single Space

4.用一个空格替换连续的空白字符

4.1. Using String.replaceAll()

4.1.使用String.replaceAll()

Now, let’s look at the other scenario. We can solve this problem in two steps:

现在,让我们来看看另一种情况。我们可以分两步解决这个问题。

  • replacing consecutive whitespace with one single space
  • trimming the result of the first step

It’s worth mentioning that we can also first trim the input string and then replace consecutive whitespace. So, it doesn’t matter which step we take first.

值得一提的是,我们也可以先修剪输入字符串,然后再替换连续的空白。所以,我们先采取哪一步并不重要。

For the first step, we can still use replaceAll() with a regex to match consecutive whitespace characters and set one space as the replacement.

对于第一步,我们仍然可以使用replaceAll()与一个regex来匹配连续的空白字符,并设置一个空格作为替换。

The regex ‘\s+’ matches one or more whitespace characters. Therefore, we can call the replaceAll(“\\s+”, ” “) method to finish the first step. Then, we can invoke the String.trim() method to apply the trim operation.

regex ‘\s+’ 匹配一个或多个空白字符。因此,我们可以调用replaceAll(“\\s+”, ” “)方法来完成第一步的工作。然后,我们可以调用String.trim()方法来应用修剪操作。

Next, let’s create a test to check if our idea can solve the problem. To make it clear, we write two assertions for the two steps:

接下来,让我们创建一个测试来检查我们的想法是否能解决问题。为了明确这一点,我们为这两个步骤写了两个断言。

String result = myString.replaceAll("\\s+", " ");
assertThat(result).isEqualTo(" I am a wonderful String ! ");
assertThat(result.trim()).isEqualTo("I am a wonderful String !");

If we give it a run, the test passes. So, the approach works as expected.

如果我们让它运行一下,测试就会通过。因此,该方法如预期的那样工作。

Next, let’s solve the problem using the Apache Commons Lang 3 library.

接下来,让我们用Apache Commons Lang 3库来解决这个问题。

4.2. Using the Apache Commons Lang3 Library

4.2.使用Apache Commons Lang3库

The StringUtils.normalizeSpace() method trims the input string and then replaces sequences of whitespace characters with a single space. Therefore, we can directly call this method to solve the problem:

StringUtils.normalizeSpace()方法对输入的字符串进行修剪,然后用一个空格来替换空白字符序列。因此,我们可以直接调用这个方法来解决这个问题。

String result = StringUtils.normalizeSpace(myString);
assertThat(result).isEqualTo("I am a wonderful String !");

The test passes if we execute it. As we can see, StringUtils.normalizeSpace() is pretty straightforward to use.

如果我们执行它,测试就会通过。正如我们所看到的,StringUtils.normalizeSpace()是相当直接的使用。

5. Conclusion

5.总结

In this article, we’ve learned how to remove whitespace characters from a string in Java.

在这篇文章中,我们已经学会了如何在Java中删除字符串中的空白字符。

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

一如既往,完整的源代码可在GitHub上获得