Java基礎十二

來源:互聯網
上載者:User

標籤:功能   截取   rev   col   set   替換   指定   定位   index   

1 StringBuffer1.1 StringBuffer的概述
  • 如果我們需要對字串進行拼接操作,每次拼接,都會構建一個新的String對象,既耗時,又浪費空間。而StringBuffer就可以解決這個問題。
  • StringBuffer是安全執行緒的字元序列。
 1.2 構造方法
  • 構造方法:無參數構造方法
public StringBuffer()

 

  • 構造方法:指定字串緩衝區的容量
public StringBuffer(int capacity)

 

  • 構造方法:指定字串緩衝區的字串內容
public StringBuffer(String str)

 

  • 樣本:
package com.xuweiwei;public class StringBufferDemo {    public static void main(String[] args) {        //public StringBuffer()        StringBuffer sb = new StringBuffer();        System.out.println("sb:"+sb);        System.out.println("sb的容量:"+sb.capacity());        System.out.println("sb的長度:"+sb.length());        //public StringBuffer(int capacity)        StringBuffer sb2 = new StringBuffer(50);        System.out.println("sb2:"+sb2);        System.out.println("sb2的容量:"+sb2.capacity());        System.out.println("sb2的長度:"+sb2.length());        //public StringBuffer(String str)        StringBuffer sb3 = new StringBuffer("hello");        System.out.println("sb3:"+sb3);        System.out.println("sb3的容量:"+sb3.capacity());        System.out.println("sb3的長度:"+sb3.length());    }}
1.3 StringBuffer的添加功能
  • 方法:可以把任意類型的資料添加到字串緩衝區,並返回字串緩衝區本身,此方法有重載方法
public StringBuffer append(String str)

 

  • 方法:在指定位置將任意類型的資料插入到字串緩衝區裡,並返回字串緩衝區本身,此方法有重載方法
public StringBuffer insert(int offset,String str)

 

  • 樣本:
package com.xuweiwei;public class StringBufferDemo2 {    public static void main(String[] args) {        StringBuffer sb = new StringBuffer();        //public StringBuffer append(String str)        sb.append("hello");        sb.append(true);        sb.append(12);        sb.append(‘a‘);        System.out.println("sb:" + sb);        sb.insert(0,‘d‘);        System.out.println("sb:" + sb);    }}
1.4 StringBuffer的刪除功能
  • 方法:刪除指定位置的字元,注意刪除的是一個,並返回字串緩衝區本身
public StringBuffer deleteCharAt(int index)

 

  • 方法:刪除指定範圍內的內容,並返回字串緩衝區本身
public StringBuffer delete(int start,int end)

 

  • 樣本:
package com.xuweiwei;public class StringBufferDemo2 {    public static void main(String[] args) {        StringBuffer sb = new StringBuffer();        sb.append("hello");        sb.append(true);        sb.append(12);        sb.append(‘a‘);        sb.insert(0, ‘d‘);        System.out.println("sb:"+sb);        sb.deleteCharAt(0);        System.out.println("sb:"+sb);        sb.delete(1,2);        System.out.println("sb:"+sb);    }}
1.5 StringBuffer的替換功能
  • 方法:從start到end,用str替換,返回字串緩衝區對象
public StringBuffer replace(int start, int end,String str)

 

  • 樣本:
package com.xuweiwei;public class StringBufferDemo2 {    public static void main(String[] args) {        StringBuffer sb = new StringBuffer();        sb.append("hello").append("world").append("java");        sb.replace(5, 10, "你好啊,世界");        System.out.println("sb:" + sb);    }}
1.6 StringBuffer的反轉功能
  • 方法:
public StringBuffer reverse()

 

  • 樣本:
package com.xuweiwei;public class StringBufferDemo2 {    public static void main(String[] args) {        StringBuffer sb = new StringBuffer();        sb.append("hello").append("world").append("java");        System.out.println("sb:" + sb.reverse());    }}
1.7 StringBuffer的截取功能
  • 方法:從指定位置開始截取內容,返回字串對象
public String substring(int start)

 

  • 方法:從指定位置開始截取到指定位置結束,返回字串對象
public String substring(int start,int end)

 

  • 樣本:
package com.xuweiwei;public class StringBufferDemo2 {    public static void main(String[] args) {        StringBuffer sb = new StringBuffer();        sb.append("hello").append("world").append("java");        System.out.println("sb:" + sb);        String str = sb.substring(3);        System.out.println("sb:" + sb);        System.out.println("str:" + str);            }}

 

Java基礎十二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.