如何在Java 9中的JShell中实现String实用程序和不变性?
1)StringUtility:String提供了几种内置的实用程序方法。诸如indexOf(),lastIndexOf(),startsWith(),startsWith(),isEmpty(),equals(),equalsIgnoreCase()之类的方法就是字符串实用程序的一部分。
在下面的代码片段中,String类的concat()方法将两个String对象的内容合并为一个。但是,“str”所指的原始值保持不变。concat()方法将创建一个新的字符串对象。就像concat()方法一样,其他String方法(例如toUpperCase(),toLowerCase()和trim()方法)都会返回新的String对象。
片段2
jshell> String str = "Nhooo";
str ==> "Nhooo"
jshell> str.concat(" is e-learning app");
$3 ==> "Nhooo is e-learning app"
jshell> str
str ==> "Nhooo" ^
jshell> String str1 = str.concat(".")
str1 ==> "Nhooo."
jshell> str1
str1 ==> "Nhooo."
jshell> String str = str.concat(" is e-learning app");
str ==> "Nhooo is e-learning app"
jshell> str
str ==> "Nhooo is e-learning app"
jshell> String str1 = "Nhooo";
str1 ==> "Nhooo"
jshell> str1.concat(" is e-learning app");
$2 ==> "Nhooo is e-learning app"
jshell> str1
str1 ==> "Nhooo"
jshell> String str2 = str1.concat(" is e-learning app");
str2 ==> "Nhooo is e-learning app"
jshell> str1
str1 ==> "Nhooo"
jshell> String str3 = str2.concat(".");
str3 ==> "Nhooo is e-learning app."
jshell> str1
str1 ==> "Nhooo"
jshell> str2
str2 ==> "Nhooo is e-learning app"
jshell> String s = "Nhooo is e-learning app."
s ==> "Nhooo is e-learning app."
jshell> s.toUpperCase()
$10 ==> "nhooo IS E-LEARNING APP."
jshell> s.toLowerCase()
$11 ==> "nhooo is e-learning app."热门推荐
10 简短新婚铺被子祝福语
11 韩语贺卡祝福语简短版
12 项目建设春节祝福语简短
13 感动妈妈的祝福语简短
14 关云生字祝福语简短
15 新老师祝福语 简短独特
16 阳历新年祝福语大全 简短
17 毕业结婚祝福语简短精辟
18 送菜的祝福语简短霸气