扫码一下
查看教程更方便
Java 11 为 String 引入了多项增强功能。
考虑以下示例
import java.util.ArrayList;
import java.util.List;
public class APITester {
public static void main(String[] args) {
String sample = " abc ";
System.out.println(sample.repeat(2)); // " abc abc "
System.out.println(sample.isBlank()); // false
System.out.println("".isBlank()); // true
System.out.println(" ".isBlank()); // true
System.out.println(sample.strip()); // "abc"
System.out.println(sample.stripLeading()); // "abc "
System.out.println(sample.stripTrailing()); // " abc"
sample = "This\nis\na\nmultiline\ntext.";
List<String> lines = new ArrayList<>();
sample.lines().forEach(line -> lines.add(line));
lines.forEach(line -> System.out.println(line));
}
}
上述代码运行结果如下
abc abc
false
true
true
abc
abc
abc
This
is
a
multiline
text.