本文共 4147 字,大约阅读时间需要 13 分钟。
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类中添加了四个新方法。 在本教程中,我们将详细研究这些新方法。
This method adjusts the indentation of each line in the string based on the value of ‘n’ and also normalizes line termination characters.
此方法根据'n'的值调整字符串中每行的缩进量,并规范行终止符。
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 PankajHow are you?***** ***** Hi Hello Pankaj How are you? **********HiHello PankajHow are you?*****
Let’s look into the same examples through .
让我们通过看同样的例子。
➜ ~ jshell| Welcome to JShell -- Version 12| For an introduction type: /help introjshell> 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。
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 to implement the .
让我们看一个示例,在该示例中,我们将使用transform()方法将CSV字符串转换为字符串列表。 注意使用实现 。
String s = "Hi,Hello,Howdy";List strList = s.transform(s1 -> {return Arrays.asList(s1.split(","));});System.out.println(strList);
Output:
输出:
Java 12 has introduced Constants API in . 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在引入了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]HelloHello
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"
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方法在正常的开发相关任务中没有太多用处。
翻译自:
转载地址:http://jnlzd.baihongyu.com/