Java 11:字符串类中的新方法

Java 11 doesn’t have a lot of language specific features. So, it was surprising to see 6 new methods getting introduced in Java String Class.

Java 11没有很多特定于语言的功能。 因此,令人惊讶的是Java String Class中引入了6种新方法。

Java 11字符串类新方法 (Java 11 String Class New Methods)

Let’s look at these new String class methods one by one.

让我们一一看一下这些新的String类方法。

  1. isBlank(): This method returns true if the string is empty or contains only white spaces code points.

    isBlank() :如果字符串为空或仅包含空格代码点,则此方法返回true。
  2. String s = "abc";
    System.out.println(s.isBlank());
    s = "";
    System.out.println(s.isBlank());
    s = "\t \t";
    System.out.println(s.isBlank());

    Output:

    输出:

    false
    true
    true

    Notice that “\t” is considered as a white space character codepoint in Unicode.

    请注意,“ \ t ”被视为Unicode中的空白字符代码点。

    Java String IsBlank()

    Java String IsBlank()

    Java字符串IsBlank()

    I am using jShell to execute the code snippets without actually creating a java file.

    我正在使用jShell执行代码片段,而没有实际创建Java文件。

  3. lines(): This method returns a stream of lines extracted from the string, separated by line terminators such as \n, \r etc.

    lines() :此方法返回从字符串中提取的行流,并用\ n,\ r等行终止符分隔。
  4. String s1 = "Hi\nHello\rWassup";
    System.out.println(s1);
    List lines = s1.lines().collect(Collectors.toList());
    System.out.println(lines);

    Output:

    输出:

    Java String lines() Function

    Java String lines() Function

    Java字符串lines()函数

    This method is useful to process multi-line strings with one line at a time.

    此方法对于一次处理多行字符串很有用。

  5. strip(), stripLeading(), stripTrailing(): These methods are used to strip whitespaces from the string. As the name suggests, strip() will remove leading and trailing whitespaces. However, stripLeading() will remove only leading whitespaces and stripTrailing() will remove only trailing whitespaces.

    strip(),stripLeading(),stripTrailing() :这些方法用于从字符串中去除空格。 顾名思义, strip()将删除前导和尾随空格。 但是, stripLeading()将仅删除前导空格,而stripTrailing()将仅删除尾随空格。
  6. String s2 = "  Hello,  \tWorld\t ";
    System.out.println("#" + s2 + "#");
    System.out.println("#" + s2.strip() + "#");
    System.out.println("#" + s2.stripLeading() + "#");
    System.out.println("#" + s2.stripTrailing() + "#");

    Output:

    输出:

    #  Hello,  	World	 #
    #Hello,  	World#
    #Hello,  	World	 #
    #  Hello,  	World#
    Java String strip(), stripLeading(), stripTrailing() functions

    Java String strip(), stripLeading(), stripTrailing()

    Java字符串strip(),stripLeading(),stripTrailing()

  7. repeat(int n): This method returns a new string whose value is the concatenation of this string repeated ‘n’ times.

    repeat(int n) :此方法返回一个新字符串,该字符串的值是该字符串的重复n次的串联。
  8. String s3 = "Hello\n";
    System.out.println(s3.repeat(3));
    s3 = "Do";
    System.out.println(s3.repeat(2));

    Output:

    输出:

    Java String repeat()

    Java String repeat()

    Java字符串repeat()

结论 (Conclusion)

Java String class has a lot of utility methods. However, all these new utility methods will be very useful because we won’t have to worry about writing them ourselves and thinking about whether they cover all the rare scenarios related to different types of Unicode characters or not.

Java String类具有许多实用程序方法。 但是,所有这些新的实用程序方法将非常有用,因为我们不必担心自己编写它们,也不必考虑它们是否涵盖了与不同类型的Unicode字符相关的所有罕见情况。

GitHub Repository. GitHub Repository中查看更多String示例。

Reference: Java 11 String Class API Doc

参考: Java 11字符串类API文档

翻译自: https://www.journaldev.com/26288/java-11-new-methods-in-string-class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值