Java 12字符串方法

Java 12 was released in March 2019. There are four new methods added in String class. In this tutorial, we will look into these new methods in detail.

Java 12于2019年3月发布。String类中添加了四个新方法。 在本教程中,我们将详细研究这些新方法。

1.缩进 (1. indent(int n))

This method adjusts the indentation of each line in the string based on the value of ‘n’ and also normalizes line termination characters.

此方法根据'n'的值调整字符串中每行的缩进量,并规范行终止符。

  • If n > 0, then n spaces (U+0020) are inserted at the beginning of each line.

    如果n> 0,则在每行的开头插入n个空格(U + 0020)。
  • If n < 0, then up to n white space characters are removed from the beginning of each line. If a given line does not contain sufficient white space then all leading white space characters are removed. The tab character is also treated as a single character.

    如果n <0,则从每行开头最多删除n个空格字符。 如果给定的行没有足够的空格,那么将删除所有前导空格字符。 制表符也被视为单个字符。
  • If n = 0, then the line remains unchanged. However, line terminators are still normalized.

    如果n = 0,则该行保持不变。 但是,行终止符仍被标准化。
String str = "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****";

System.out.println(str.indent(0));
System.out.println(str.indent(3));
System.out.println(str.indent(-3));

Output:

输出:

*****
  Hi
  	Hello Pankaj
How are you?
*****

   *****
     Hi
     	Hello Pankaj
   How are you?
   *****

*****
Hi
Hello Pankaj
How are you?
*****

Let’s look into the same examples through jshell.

让我们通过jshell看同样的例子。

➜  ~ jshell
|  Welcome to JShell -- Version 12
|  For an introduction type: /help intro

jshell> String str = "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****";
str ==> "*****\n  Hi\n  \tHello Pankaj\rHow are you?\n*****"

jshell> str.indent(0)
$2 ==> "*****\n  Hi\n  \tHello Pankaj\nHow are you?\n*****\n"

jshell> str.indent(3)
$3 ==> "   *****\n     Hi\n     \tHello Pankaj\n   How are you?\n   *****\n"

jshell> str.indent(-3)
$4 ==> "*****\nHi\nHello Pankaj\nHow are you?\n*****\n"

jshell>

Notice that \r is being normalized to \n when indent() method is called.

注意,调用indent()方法时,\ r被标准化为\ n。

2. transform(Function <?super String,?扩展R> f) (2. transform(Function<? super String,​? extends R> f))

This method allows us to call a function on the given string. The function should expect a single String argument and produce an R result.

此方法使我们可以在给定的字符串上调用函数。 该函数应该期望一个String参数,并产生一个R结果。

Let’s look at an example where we will use transform() method to convert a CSV string to the list of strings. Notice the use of lambda expressions to implement the functional interface.

让我们看一个示例,在该示例中,我们将使用transform()方法将CSV字符串转换为字符串列表。 注意使用lambda表达式实现功能接口

String s = "Hi,Hello,Howdy";
List strList = s.transform(s1 -> {return Arrays.asList(s1.split(","));});
System.out.println(strList);

Output:

输出:

3.可选的<String> describeConstable() (3. Optional<String> describeConstable())

Java 12 has introduced Constants API in JEP 334. If you look at the String class documentation, it implements two new interfaces from Constants API – Constable, and ConstantDesc. This method is declared in the Constable interface and implemented in the String class.

Java 12在JEP 334中引入了Constants API。 如果您查看String类文档,它将实现Constants API的两个新接口-Constable和ConstantDesc。 此方法在Constable接口中声明,并在String类中实现。

This method returns an Optional containing the nominal descriptor for this instance, which is the instance itself.

此方法返回一个Optional,其中包含该实例的名义描述符,即实例本身。

String so = "Hello";
Optional os = so.describeConstable();
System.out.println(os);
System.out.println(os.get());

Output:

输出:

Optional[Hello]
Hello
Hello
Java String Method DescribeConstable

Java String Method DescribeConstable

Java字符串方法DescribeConstable

4.字符串resolveConstantDesc((MethodHandles.Lookup查找) (4. String resolveConstantDesc​(MethodHandles.Lookup lookup))

This method is part of Constants API and declared in ConstantDesc interface. It resolves this instance as a ConstantDesc, the result of which is the instance itself.

此方法是Constants API的一部分,并在ConstantDesc接口中声明。 它将实例解析为ConstantDesc,其结果就是实例本身。

jshell> import java.lang.invoke.MethodHandles;

jshell> String so1 = "Hello";
so1 ==> "Hello"

jshell> so1.resolveConstantDesc(MethodHandles.lookup());
$18 ==> "Hello"

结论 (Conclusion)

The indent() and transform() methods are a great addition to the String class. The Constants API methods don’t have much usage for normal development related tasks.

indent()和transform()方法是String类的一个很好的补充。 Constants API方法在正常的开发相关任务中没有太多用处。

翻译自: https://www.journaldev.com/28673/java-12-string-methods

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值