Java 11 String API Additions – Java 11 字符串 API 的新增功能

最后修改: 2018年 12月 18日

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

1. Introduction

1.引言

Java 11 added few useful APIs to the commonly used String class. In this tutorial, we will explore and use these new APIs.

Java 11为常用的String增加了一些有用的API。在本教程中,我们将探索并使用这些新的API。

2. repeat()

2.repeat()

As the name suggests, the repeat() instance method repeats the string content.

顾名思义,repeat()实例方法会重复字符串内容。

It returns a string whose value is the concatenation of the string repeated n times, where n is passed as a parameter:

它返回一个字符串,其值是重复n次的字符串的连接,其中n被作为一个参数传递

@Test
public void whenRepeatStringTwice_thenGetStringTwice() {
    String output = "La ".repeat(2) + "Land";

    is(output).equals("La La Land");
}

Additionally, repeat() returns an empty string if the string is empty or the count is zero.

此外,repeat()如果字符串为空或计数为零,则返回一个空字符串。

3. strip*()

3.strip*()

The strip() instance method returns a string with all leading and trailing whitespaces removed:

strip()实例方法返回一个去除所有前导和尾部白点的字符串

@Test
public void whenStripString_thenReturnStringWithoutWhitespaces() {
    is("\n\t  hello   \u2005".strip()).equals("hello");
}

Java 11 also added methods stripLeading() and stripTrailing(), which handle leading and trailing whitespaces, respectively.

Java 11还增加了stripLeading()stripTrailing()方法,它们分别处理前导和尾部的空白。

3.1. Difference Between strip() and trim()

3.1.strip()trim()之间的区别

strip*() determines whether the character is whitespace or not based on Character.isWhitespace(). In other words, it is aware of Unicode whitespace characters.

strip*()根据Character.isWhitespace()来决定该字符是否为空白字符。换句话说,它知道Unicode的空白字符

This is different from trim(), which defines space as any character that is less than or equal to the Unicode space character (U+0020). If we use trim() in the previous example, we will get a different result:

这与trim()不同,后者将空格定义为小于或等于Unicode空格字符(U+0020)的任何字符。如果我们在前面的例子中使用trim(),我们会得到一个不同的结果。

@Test
public void whenTrimAdvanceString_thenReturnStringWithWhitespaces() {
    is("\n\t  hello   \u2005".trim()).equals("hello   \u2005");
}

Notice how trim() was able to trim the leading whitespace, but it didn’t trim the trailing whitespace. This is because trim() is not aware of Unicode whitespace characters and hence does not consider ‘\u2005′ a whitespace character.

注意到trim()如何能够修剪前面的空白,但它并没有修剪后面的空白。这是因为trim()没有意识到Unicode的空白字符,因此不认为’是空白字符。

4. isBlank()

4.isBlank()

The isBlank() instance method returns true if the string is empty or contains only whitespace. Otherwise, it returns false:

isBlank()实例方法在字符串为空或只包含空白时返回true。否则,它返回false

@Test
public void whenBlankString_thenReturnTrue() {
    assertTrue("\n\t\u2005  ".isBlank());
}

Similarly, the isBlank() method is aware of Unicode whitespace characters, just like strip().

同样,isBlank()方法也知道Unicode空白字符,就像strip()

5. lines()

5.lines()

The lines() instance method returns a Stream of lines extracted from the string, separated by line terminators:

lines()实例方法返回一个Stream 从字符串中提取的行,用行结束符分隔

@Test
public void whenMultilineString_thenReturnNonEmptyLineCount() {
    String multilineStr = "This is\n \n a multiline\n string.";

    long lineCount = multilineStr.lines()
      .filter(String::isBlank)
      .count();

    is(lineCount).equals(3L);
}

A line terminator is one of the following: “\n”, “\r”, or “\r\n”.

行终结符是以下之一。“\n”, “\r”,“\r\n”

The stream contains lines in the order in which they occur. The line terminator is removed from each line.

流中的行按其出现的顺序排列。每一行的行结束符都被删除。

This method should be preferred over split(), as it provides better performance for breaking multi-line input.

这个方法应该比split()更受青睐,因为它为断开多行输入提供了更好的性能。

6. Conclusion

6.结论

In this quick article, we explored the new String APIs in Java 11.

在这篇快速文章中,我们探讨了Java 11中新的字符串API。

Finally, code snippets can be found over on GitHub.

最后,可以在GitHub上找到代码片段