好程序员Java教程分享Java中String类型的10个问题
简单来说,“==”是用来检测俩引用是不是指向内存中的同一个对象,而equals()方法则检测的是两个对象的值是否相等。只要你想检测俩字符串是不是相等的,你就必须得用equals()方法。
如果你知道“字符串保留(string intern)”的概念那就更好了。
String对象是不可变的就意味着直到垃圾回收器过来清扫之前它们都不会发生变化的。用数组的话,就可以很明确的修改它任何位置的字符元素。这样的话,如密码等安全敏感的信息就不会出现在系统的任何地方。
从JDK7开始的话,我们就可以在switch条件表达式中使用字符串了,也就是说7之前的版本是不可以的。
1<span
class="hljs-comment"
style="color: rgb(153, 153, 136);">// java 7 only!</span>
2<span
class="hljs-keyword"
style="font-weight: bold;">switch</span> (str.toLowerCase()) {
3
<span
class="hljs-keyword"
style="font-weight: bold;">case</span> <spanclass="hljs-string"
style="color: rgb(221, 17, 68);">"a"</span>:
4
<span
class="hljs-keyword"
style="font-weight: bold;">value</span> = <spanclass="hljs-number"
style="color: rgb(0, 128, 128);">1</span>;
5
<span
class="hljs-keyword"
style="font-weight: bold;">break</span>;
6
<span
class="hljs-keyword"
style="font-weight: bold;">case</span> <spanclass="hljs-string"
style="color: rgb(221, 17, 68);">"b"</span>:
7
<span
class="hljs-keyword"
style="font-weight: bold;">value</span> = <spanclass="hljs-number"
style="color: rgb(0, 128, 128);">2</span>;
8
<span
class="hljs-keyword"
style="font-weight: bold;">break</span>;
9}
1<span class="hljs-keyword" style="font-weight: bold;">int</span> n = Integer.parseInt(<span class="hljs-string" style="color: rgb(221, 17, 68);">"10"</span>);
如此简单,经常使用有偶尔也会被遗忘。
我们可以很便捷的使用正则表达式来进行分隔。“\s”就表示空格,还有如””,”\t”,”\r”,”\n”.
1<span class="hljs-type" style="color: rgb(68, 85, 136); font-weight: bold;">String</span>[] strArray = aString.<span class="hljs-built_in"style="color: rgb(0, 134, 179);">split</span>(<span class="hljs-string"style="color: rgb(221, 17, 68);">"\\s+"</span>);
在JDK6中,这个方法只会在标识现有字符串的字符数组上 给一个窗口来表示结果字符串,但是不会创建一个新的字符串对象。如果需要创建个新字符串对象,可以这样在结果后面+一个空的字符串:
1str.substring(m, n) + <span class="hljs-string" style="color: rgb(221, 17, 68);">""</span>
这么写的话就会创建一个新的字符数组来表示结果字符串。同时,这么写也有一定的几率让你的代码跑的更快,因为垃圾回收器会吧没有在使用的大字符串回收而留下子字符串。
Oracle JDK7中的substring()方法会创建一个新的字符数组,而不用之前存在的。看看这张图就会明白substring()方法在JDK6和JDK7中的区别。
String vs StringBuilder:StringBuilder是可变的,这就意味你在创建对象之后还可以去修改它的值。StringBuilder vs StringBuffer:StringBuffer是同步的,意味着它是线程安全的,但是就会比StringBuilder慢些。
在Python编程中,只需要用字符串去乘以一个数字就可以 搞定了,那在Java编程中,我们可以使用来自Apache Commons Lang包中的StringUtils类的repeat()方法。
1<span
class="hljs-built_in"
style="color: rgb(0, 134, 179);">String</span> str = <span
class="hljs-string"
style="color: rgb(221, 17, 68);">"abcd"</span>;
2<span
class="hljs-built_in"
style="color: rgb(0, 134, 179);">String</span> repeated = StringUtils.repeat(str,<span
class="hljs-number"
style="color: rgb(0, 128, 128);">3</span>);
3<span
class="hljs-comment"
style="color: rgb(153, 153, 136);">//abcdabcdabcd</span>
1<span
class="hljs-built_in"
style="color: rgb(0, 134, 179);">String</span> str = <span
class="hljs-string"
style="color: rgb(221, 17, 68);">"Sep 17, 2013"</span>;
2<span
class="hljs-built_in"
style="color: rgb(0, 134, 179);">Date</span> date = <span
class="hljs-keyword"
style="font-weight: bold;">new</span> SimpleDateFormat(<span
class="hljs-string"
style="color: rgb(221, 17, 68);">"MMMM d, yy"</span>, Locale.ENGLISH).parse(str);
3System.out.println(date);
4<span
class="hljs-comment"
style="color: rgb(153, 153, 136);">//Tue Sep 17 00:00:00 EDT 2013</span>
使用Apache Commons Lang包中的 StringUtils类就可以完成这个工作。
1<span
class="hljs-keyword"
style="font-weight: bold;">int</span> n = StringUtils.countMatches(<span
class="hljs-string"
style="color: rgb(221, 17, 68);">"11112222"</span>, <span
class="hljs-string"
style="color: rgb(221, 17, 68);">"1"</span>);
2System.out.printl<span
class="hljs-string"
style="color: rgb(221, 17, 68);">``</span><span
class="hljs-string"
style="color: rgb(221, 17, 68);">` n(n);</span>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69913892/viewspace-2663351/ ,如需转载,请注明出处,否则将追究法律责任。