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:

输出:

*****HiHello Pankaj
How are you?
**********HiHello PankajHow 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 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 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字符串方法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

Java 12字符串方法相关推荐

  1. JAVA截取字符串方法

    //截取字符串方法 public static void main(String[] args) { String str="QW/E1/2/45"; //寻找最后一个" ...

  2. java常用字符串方法_Java常用字符串方法小结

    下面是对字符串操作的代码小总结.大部分是String类的 操作方法,需要的朋友可以参考下 public class StudyString { public static void main(Stri ...

  3. java解析字符串方法_java字符串的截取方法substring()代码解析

    这篇文章主要介绍了java字符串的截取方法substring()代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 返回位于 String 对象 ...

  4. java url substring,Java截取字符串方法subString方法

    String 类的 substring() 方法用于对字符串进行提取,该方法主要有两种重载形式,下面分别介绍. 1. substring(int beginIndex) 形式 此方式用于提取从索引位置 ...

  5. php如何让字符串变运算公式,MySQL_使用java处理字符串公式运算的方法,  在改进一个关于合同的项 - phpStudy...

    使用java处理字符串公式运算的方法 在改进一个关于合同的项目时,有个需求,就是由于合同中非数据项的计算公式会根据年份而进行变更,而之前是将公式硬编码到系统中的,只要时间一变,系统就没法使用了,因此要 ...

  6. 使用java中replaceAll方法替换字符串中的反斜杠

    今天在项目中使用java中replaceAll方法将字符串中的反斜杠("\")替换成空字符串(""),结果出现如下的异常: 1 java.util.regex. ...

  7. 判断字符串不包含某个字符php,java判断字符串是否包含某个字符的方法

    java判断字符串是否包含某个字符的方法: 一.contains方法 1:描述 java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列 2: ...

  8. java 获取字符串长度_ava练习实例:java字符串长度与Java String charAt() 方法 (建议收藏)...

    JJava 字符串长度 public class StringDemo {public static void main(String args[]) {String palindrome = &qu ...

  9. Java 判断字符串是否为空的四种方法、优缺点与注意事项

    以下是Java 判断字符串是否为空的四种方法: 方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equals(s)); 方法二: ...

最新文章

  1. Bzoj4566:[HAOI2016]找相同字符
  2. 啧啧,这种程序员……| 每日趣闻
  3. Arrays工具类(jre中基本类库提供的工具类)
  4. nginx mozilla_我发现Mozilla的私人浏览模式存在重大缺陷。
  5. 1 io口 stm32_从STM32分享各种硬件以及总线之GPIO简介
  6. eos 编译笔记(注意点)
  7. 两种方法求最大公约数和最小公倍数
  8. maven GroupId 和ArtifactId通常填什么
  9. 浏览器升级怎么升_测评:iOS 13 升不?新老机型跑分对比!
  10. Silverlight查询大数据出错?使用服务器端分页控件DomainDataSource
  11. Codeforce 1700Difficulty Graphs 20 questions
  12. 思维导图的了解和使用
  13. 补天漏洞平台为什么能吸引众多白帽和企业?
  14. shiro:principal为空问题
  15. 一个大学毕业生的反思
  16. 多边形区域填充算法--扫描线填充算法(有序边表法)
  17. 使用javac编译单个Java文件
  18. FPGA如何利用查表法得到某角度所对应的正弦值、余弦值
  19. java题目:振兴中华
  20. 简单操作破解PDF加密文件

热门文章

  1. 作业四: 结对编程项目---四则运算
  2. Flask+Mysql搭建网站之数据库问题
  3. NSArray 的遍历
  4. Mvc6 错误Microsoft.AspNet.Http.Features.IRequestIdentifierFeature
  5. 华彬 - 华彬讲透孙子兵法(2015年5月22日)
  6. 利用Ajax实现DataGrid无刷新分页(AjaxGrid)【转】
  7. 手机网页 复制信息方法 免费短信
  8. 数据结构 7并查集(DISJOINT SET)
  9. CMake 学习笔记 02 - 更复杂的项目
  10. MVC部分视图的使用