博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 12字符串方法
阅读量:2530 次
发布时间:2019-05-11

本文共 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类中添加了四个新方法。 在本教程中,我们将详细研究这些新方法。

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 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。

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 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:

输出:

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

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
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方法在正常的开发相关任务中没有太多用处。

翻译自:

转载地址:http://jnlzd.baihongyu.com/

你可能感兴趣的文章
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>