java中經常使用的String方法

<div id="cnblogs_post_body" class="blogpost-body"><p><span style="font-size: 18px"><strong><span style="font-family: &quot;Microsoft YaHei&quot;">1 length()字符串的長度</span></strong></span></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word!";<span style="color: #000000"> System.out.println(a.length);</span></span></pre> </div> <p><span style="font-size: 16px; font-family: 黑體">輸出的結果是字符串長度10。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">2 charAt()截取一個字符</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; System.out.println(a.charAt(</span>1));</span></pre> </div> <p><span style="font-size: 16px"><span style="font-family: 黑體">輸出的結果是字符串a的下標爲1的字符e</span>。</span></p> <p>&nbsp;</p> <p><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px"><strong>3 getchars()截取多個字符並由其餘字符串接收</strong></span></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; </span><span style="color: #0000ff">char</span>[] b = <span style="color: #0000ff">new</span> <span style="color: #0000ff">char</span>[10<span style="color: #000000">]; a.getChars(</span>0, 5, b, 0<span style="color: #000000">); System.out.println(b);</span></span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果爲Hello,其中第一個參數0是要截取的字符串的初始下標(int sourceStart),第二個參數5是要截取的字符串的結束後的下一個下標(int sourceEnd)也就是實際截取到的下標是int sourceEnd-1,第三個參數是接收的字符串(char target[]),最後一個參數是接收的字符串開始接收的位置。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">4&nbsp;getBytes()將字符串變成一個byte數組</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; </span><span style="color: #0000ff">byte</span> b[] =<span style="color: #000000"> a.getBytes(); System.out.println(</span><span style="color: #0000ff">new</span> String(b));</span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果爲Hello Word的byte數組。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">5&nbsp;toCharArray()將字符串變成一個字符數組</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; </span><span style="color: #0000ff">char</span>[]b =<span style="color: #000000"> a.toCharArray(); System.out.println(b); </span></span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果爲Hello Word字符數組。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">6&nbsp;equals()和<strong>equalsIgnoreCase()</strong>比較兩個字符串是否相等,前者區分大小寫,後者不區分</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; String b </span>= "hello word"</span><span style="color: #000000"><span style="background-color: #ccffcc">; System.out.println(a.equals(b));<br>System.out.println(a.equalsIgnoreCase(b)); </span> </span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果爲第一條爲false,第二條爲true。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">7&nbsp;startsWith()和endsWith()判斷字符串是否是以特定的字符開頭或結束</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; System.out.println(a.startsWith(</span>"ee"<span style="color: #000000">)); System.out.println(a.endsWith(</span>"rd"));</span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果第一條爲false,第二條爲true。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">8&nbsp;toUpperCase()和toLowerCase()將字符串轉換爲大寫或小寫</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; System.out.println(a.toUpperCase()); System.out.println(a.toLowerCase());</span></span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果第一條爲「HELLO WORD」,第二條爲「hello word」。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">9&nbsp;concat()&nbsp;鏈接兩個字符串</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; String b </span>= "你好"<span style="color: #000000">; System.out.println(b.concat(a));</span></span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果爲「你好Hello Word」。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">10 trim()去掉起始和結束的空格</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = <span style="color: #800000">"</span><span style="color: #800000"> Hello Word </span><span style="color: #800000">"</span><span style="color: #000000">; System.</span><span style="color: #0000ff">out</span>.println(a.trim());</span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果爲「Hello Word」。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">11 substring()截取字符串</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; System.out.println(a.substring(</span>0, 5<span style="color: #000000">)); System.out.println(a.substring(</span>6));</span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果第一條爲「Hello」,第一個參數0(beginIndex)是開始截取的位置,第二個參數5(endIndex)是截取結束的位置,輸出的結果第二條是「Word」,參數6(beginIndex)是開始截取的位置。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">12&nbsp;indexOf()和lastIndexOf()前者是查找字符或字符串第一次出現的地方,後者是查找字符或字符串最後一次出現的地方</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; System.out.println(a.indexOf(</span>"o"<span style="color: #000000">)); System.out.println(a.lastIndexOf(</span>"o"));</span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果第一條是4,是o第一次出現的下標,第二條是7,是o最後一次出現的下標。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">13 compareTo()和compareToIgnoreCase()按字典順序比較兩個字符串的大小,前者區分大小寫,後者不區分</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; String b </span>= "hello word"<span style="color: #000000">; System.out.println(a.compareTo(b)); System.out.println(a.compareToIgnoreCase(b));</span></span>&nbsp;</pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果第一條爲-32,第二條爲0,兩個字符串在字典順序中大小相同,返回0。</span></p> <p>&nbsp;</p> <p><strong><span style="font-family: &quot;Microsoft YaHei&quot;; font-size: 18px">14&nbsp;replace() 替換</span></strong></p> <div class="cnblogs_code"> <pre><span style="background-color: #ccffcc">String a = "Hello Word"<span style="color: #000000">; String b </span>= "你好"<span style="color: #000000">; System.out.println(a.replace(a, b)); System.out.println(a.replace(a, </span>"HELLO WORD"));<br>System.out.println(b.replace("你", "你們"));</span></pre> </div> <p><span style="font-family: 黑體; font-size: 16px">輸出的結果第一條爲「你好」,第二條爲「HELLO WORD」,第三條爲「你們好」。</span></p> <p>&nbsp;</p></div>數組

相關文章
相關標籤/搜索