Checking for Empty or Blank Strings in Java – 在Java中检查空或空白的字符串

最后修改: 2019年 7月 3日

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

1. Introduction

1.绪论

In this tutorial, we’ll discuss some ways of checking for empty or blank strings in Java. There are some native language approaches, as well as a couple of libraries.

在本教程中,我们将讨论在Java中检查空字符串或空白字符串的一些方法。有一些本地语言的方法,也有一些库。

2. Empty vs. Blank

2.空白与空白

Of course, it’s pretty common to know when a string is empty or blank, but let’s make sure we’re on the same page with our definitions.

当然,知道一个字符串为空或空白是很常见的,但让我们确保我们的定义是一致的。

We consider a string to be empty if it’s either null or a string without any length. If a string only consists of whitespace, then we call it blank.

如果一个字符串是空的或者是一个没有任何长度的字符串,我们认为它是空的。如果一个字符串只由空格组成,那么我们称它为空白

For Java, whitespaces are characters, like spaces, tabs, and so on. We can check out Character.isWhitespace for examples.

对于Java来说,空白是指字符,像空格、制表符等等。我们可以查看Character.isWhitespace的例子。

3. Empty Strings

3.空字符串

3.1. With Java 6 and Above

3.1.使用Java 6及以上版本

If we’re at least on Java 6, then the simplest way to check for an empty string is String#isEmpty:

如果我们至少是在Java 6上,那么检查字符串的最简单方法是String#isEmpty

boolean isEmptyString(String string) {
    return string.isEmpty();
}

To make it also null-safe, we need to add an extra check:

为了使它也是无效安全的,我们需要增加一个额外的检查。

boolean isEmptyString(String string) {
    return string == null || string.isEmpty();
}

3.2. With Java 5 and Below

3.2.使用Java 5及以下版本

String#isEmpty was introduced with Java 6. For Java 5 and below, we can use String#length instead:

String#isEmpty是由Java 6引入的。对于Java 5及以下版本,我们可以使用String#length代替。

boolean isEmptyString(String string) {
    return string == null || string.length() == 0;
}

In fact, String#isEmpty is just a shortcut to String#length.

事实上,String#isEmpty只是String#length的一个快捷方式

4. Blank Strings

4.空白的字符串

Both String#isEmpty and String#length can be used to check for empty strings.

String#isEmptyString#length都可以用来检查的字符串。

If we also want to detect blank strings, we can achieve this with the help of String#trim. It’ll remove all leading and trailing whitespaces before performing the check:

如果我们还想检测空白字符串,我们可以在String#trim的帮助下实现。它将在执行检查之前删除所有前导和尾部的空白:

boolean isBlankString(String string) {
    return string == null || string.trim().isEmpty();
}

To be precise, String#trim will remove all leading and trailing characters with a Unicode code less than or equal to U+0020.

准确地说,String#trim将删除所有具有Unicode代码小于或等于U+0020的前导和尾部字符。

And also, remember that Strings are immutable, so calling trim won’t actually change the underlying string.

还有,记住Strings是不可改变的,所以调用trim实际上不会改变底层字符串。

In addition to the above approach, as of Java 11, we can also use the isBlank() method instead of trimming:

除了上述方法外,从Java 11开始,我们还可以使用isBlank()方法代替修剪

boolean isBlankString(String string) {
    return string == null || string.isBlank();
}

The isBlank() method is a bit more efficient as well, as it doesn’t create a new String on the heap. As a result, if we’re on Java 11 or above, this is the preferred approach.

isBlank()方法也更有效一些,因为它不会在堆上创建一个新的String。因此,如果我们是在Java 11或以上版本,这是首选的方法。

5. Bean Validation

5.Bean验证

Another way to check for blank strings is regular expressions. For instance, this comes in handy with Java Bean Validation:

检查空白字符串的另一种方法是正则表达式。例如,这在Java Bean Validation中很方便。

@Pattern(regexp = "\\A(?!\\s*\\Z).+")
String someString;

The given regular expression ensures that empty or blank strings won’t validate.

给出的正则表达式确保空或空白字符串不会被验证。

6. With Apache Commons

6.使用Apache Commons

If it’s ok to add dependencies, we can use Apache Commons Lang. This has a host of helpers for Java.

如果可以添加依赖项,我们可以使用Apache Commons Lang。这有很多Java的帮助工具。

If we use Maven, we need to add the commons-lang3 dependency to our pom:

如果我们使用Maven,我们需要将commons-lang3依赖添加到我们的pom中。

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

Among other things, this gives us StringUtils.

在其他方面,这给了我们StringUtils

This class comes with methods like isEmpty, isBlank, and so on:

这个类带有像isEmptyisBlank这样的方法,等等。

StringUtils.isBlank(string)

This call does the same as our own isBlankString method. It’s null-safe and also checks for whitespaces.

这个调用与我们自己的isBlankString方法的作用相同。它是安全的,也会检查空白处。

7. With Guava

7.与番石榴

Another well-known library that brings certain string related utilities is Google’s Guava. Starting with version 23.1, there are two flavors of Guava: android and jre. The Android flavor targets Android and Java 7, whereas the JRE flavor goes for Java 8.

另一个带来某些字符串相关实用程序的知名库是Google的Guava。从23.1版本开始,Guava有两种风格。androidjre。Android风味的目标是Android和Java 7,而JRE风味的目标是Java 8。

If we’re not targeting Android, we can just add the JRE flavor to our pom:

如果我们的目标不是Android,我们可以直接将JRE风味添加到我们的pom中。

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

Guavas Strings class comes with the method Strings.isNullOrEmpty:

Guavas的字符串类带有Strings.isNullOrEmpty方法。

Strings.isNullOrEmpty(string)

It checks whether a given string is null or empty, but it won’t check for whitespace-only strings.

它检查一个给定的字符串是否为空或空,但它不会检查只有空白的字符串

8. Conclusion

8.结语

There are several ways to check whether a string is empty or not. Often, we also want to check if a string is blank, meaning that it consists of only whitespace characters.

有几种方法来检查一个字符串是否为空。通常情况下,我们还想检查一个字符串是否为空白,也就是说,它只由空白字符组成。

The most convenient way is to use Apache Commons Lang, which provides helpers such as StringUtils.isBlank. If we want to stick to plain Java, we can use a combination of String#trim with either String#isEmpty or String#length. For Bean Validation, regular expressions can be used instead.

最方便的方法是使用Apache Commons Lang,它提供了诸如StringUtils.isBlank之类的助手。如果我们想坚持使用普通的Java,我们可以使用String#trimString#isEmptyString#length的组合。对于Bean Validation,可以使用正则表达式来代替。

Make sure to check out all these examples over on GitHub.

请确保在GitHub上查看所有这些例子