1. Introduction
1.导言
Working with Strings in Java is sometimes confusing because we have many ways to do similar things.
在 Java 中使用 Strings 有时会令人困惑,因为我们有很多方法来做类似的事情。
In this article, we’ll look at how to validate blank and empty Strings using the isEmpty() and isBlank() methods. Although similar, the two methods are not the same.
在本文中,我们将了解如何使用 isEmpty() 和 isBlank() 方法来验证 空白和空 Strings 。这两种方法虽然相似,但并不相同。
2. Glancing at String.isEmpty()
2.瞥一眼 String.isEmpty()
Let’s start with the isEmpty() String operation. Simply put, the isEmpty() method returns true if the String is empty. Otherwise, it returns false.
让我们从 isEmpty() String 操作开始。简单地说,如果 String 为空,isEmpty() 方法将返回 true。否则,返回 false。
Internally, isEmpty() relies on the length of the byte array that represents the text of a String object. Moreover, the isEmpty() method counts any type of character to calculate if a text is empty or not. Therefore, empty spaces, tabulations, new lines, or any character that can be represented as a byte counts as a valid character.
在内部,isEmpty() 依赖于表示 String 对象文本的 字节数组的长度。此外,isEmpty() 方法会计算任何类型的字符,以计算文本是否为空。因此,空格、制表符、新行或任何可表示为字节的字符都算作有效字符。
Let’s illustrate that with a simple test:
让我们用一个简单的测试来说明这一点:
@Test
public void givenString_whenCallIsEmpty_thenReturnCorrectValues() {
assertFalse("Example text".isEmpty());
assertTrue("".isEmpty());
assertFalse(" ".isEmpty());
assertFalse("\t\n\r\f".isEmpty());
}
Notoriously, the first line tests a String that contains characters, so isEmpty() returns false.
值得注意的是,第一行测试的 String 包含字符,因此 isEmpty() 返回 false。
On the other hand, the second String doesn’t contain any character, and thus, isEmpty() returns true.
另一方面,第二个 String 不包含任何字符,因此 isEmpty() 返回 true。
Finally, for the String with only blank characters and the one with escape characters at lines 3 and 4, isEmpty() returns false.
最后,对于仅包含空白字符的 String 和第 3 行和第 4 行包含转义字符的 String, isEmpty() 返回 false。
3. Looking at Java 11’s String.isBlank()
3.查看 Java 11 的 String.isBlank()
The isBlank() method, introduced in Java 11, is identical to isEmpty() with the nuance that it also returns true for Strings that contain only whitespace characters.
在 Java 11 中引入的 isBlank() 方法与 isEmpty() 方法相同,细微差别在于它还会为仅包含空白字符的 Strings 返回 true。
The five characters considered whitespace characters in Java are the \s (space) and the \t, \n, \r, and \f escape sequences.
在 Java 中被视为空白字符的五个字符是 /s(空格)和 /t、\n、\r 和 /f 转义序列。
Behind the scenes, the isBlank() method searches for the index of the first non-whitespace character. If there are no non-whitespace characters, that index would be equal to the length of the array. Finally, it compares that index with the length of the byte array to output the correct answer.
在幕后,isBlank() 方法会搜索第一个非空格字符的索引。如果没有非空格字符,该索引将等于数组的长度。最后,它将该索引与字节数组的长度进行比较,以输出正确答案。
Let’s check that out with a unit test:
让我们用单元测试来验证一下:
@Test
public void givenString_whenCallStringIsBlank_thenReturnCorrectValues() {
assertFalse("Example text".isBlank());
assertTrue("".isBlank());
assertTrue(" ".isBlank());
assertTrue("\t\n\r\f ".isBlank());
}
Noticeably, “Example text” is considered not blank since it contains at least one non-whitespace character.
值得注意的是,”示例文本 “不被视为空白,因为它至少包含一个非空格字符。
Additionally, the second String doesn’t contain any characters, so it’s blank.
此外,第二个 String 不包含任何字符,因此是空白的。
The String at the third line has only whitespace characters, so isBlank() returns true.
第三行的 String 只有空白字符,因此 isBlank() 返回 true。
Furthermore, the String in the last line contains all escape sequence characters that are considered as whitespaces. Therefore, isBlank() also returns true in that case.
此外,最后一行中的 String 包含所有被视为空格的 escape 序列字符。因此,在这种情况下,isBlank() 也会返回 true。
4. Comparing isBlank() vs. isEmpty()
4.isBlank() 与 isEmpty() 的比较
In summary, isEmpty() only returns true when the String doesn’t contain any character. In contrast, isBlank() returns true when the String doesn’t contain any character and all of its characters are whitespace characters.
总之,只有当 String 不包含任何字符时,isEmpty() 才会返回 true。相反,当 String 不包含任何字符且所有字符都是空白字符时,isBlank() 返回 true。
Let’s use a table to visualize all return values of isEmpty() and isBlank() in the situations described in previous sections.
让我们使用表格来直观显示 isEmpty() 和 isBlank() 在前几节所述情况下的所有返回值。
Method | no character | \t | \n | \r | \f | \s (space) | any other |
---|---|---|---|---|---|---|---|
isEmpty() | true | false | false | false | false | false | false |
isBlank() | true | true | true | true | true | true | false |
The above table summarizes that if the String doesn’t contain any character, both methods return true.
上表总结了如果 String 不包含任何字符,两种方法都会返回 true。
Additionally, the escape sequences \t, \n, \r, \f, and \s are considered whitespace characters, so only isBlank() returns true. In contrast, isEmpty() returns true for all of them.
此外,转义序列 /t、/n、/r、/f 和 /s 被视为空白字符,因此只有 isBlank() 返回 true。相比之下,isEmpty() 对所有字符都返回 true。
Finally, for any other character different from the ones shown in the table, both methods return false.
最后,对于与表中所示字符不同的任何其他字符,这两种方法都会返回 false。
Before Java 11, developers typically used the combination of String.trim() and String.isEmpty() to validate that a text contains only whitespace characters. However, as we saw throughout this tutorial, in applications using Java 11 or higher, we can simplify that to simply use String.isBlank().
在 Java 11 之前,开发人员通常使用 String.trim() 和 String.isEmpty() 的组合来验证文本是否仅包含空白字符。但是,正如我们在本教程中所看到的,在使用 Java 11 或更高版本的应用程序中,我们可以简化这一过程,只需使用 String.isBlank() 即可。
5. Conclusion
5.结论
In this tutorial, we’ve seen the differences between the isBlank() vs. isEmpty(). The critical difference is that isBlank() returns true for whitespace characters, like some escape sequences. On the other hand, isEmpty() only returns true when the String doesn’t contain any character.
在本教程中,我们了解了 isBlank() 与 isEmpty() 之间的区别。关键的区别在于,isBlank() 会为空白字符(如某些转义序列)返回 true。另一方面,isEmpty() 只有在 String 不包含任何字符时才返回 true。
As always, you can find the source code over on GitHub.