詳解Java中StringBuffer類常用方法_java

來源:互聯網
上載者:User

String是不變類,用String修改字串會建立一個String對象,如果頻繁的修改,將會產生很多的String對象,開銷很大.因此java提供了一個StringBuffer類,這個類在修改字串方面的效率比String高了很多。
在java中有3個類來負責字元的操作。

  •   1.Character 是進行單個字元操作的,
  •   2.String 對一串字元進行操作,不可變類。
  •   3.StringBuffer 也是對一串字元進行操作,但是可變類。
public class UsingStringBuffer {   /**    * 尋找匹配字串    */   public static void testFindStr() {     StringBuffer sb = new StringBuffer();     sb.append("This is a StringBuffer");     // 返回子字串在字串中最先出現的位置,如果不存在,返回負數     System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is"));     // 給indexOf方法設定參數,指定匹配的起始位置     System.out.println("sb.indexOf(\"is\")=" + sb.indexOf("is", 3));     // 返回子字串在字串中最後出現的位置,如果不存在,返回負數     System.out.println("sb.lastIndexOf(\"is\") = " + sb.lastIndexOf("is"));     // 給lastIndexOf方法設定參數,指定匹配的結束位置     System.out.println("sb.lastIndexOf(\"is\", 1) = "         + sb.lastIndexOf("is", 1));   }    /**    * 截取字串    */   public static void testSubStr() {     StringBuffer sb = new StringBuffer();     sb.append("This is a StringBuffer");     // 預設的終止位置為字串的末尾     System.out.print("sb.substring(4)=" + sb.substring(4));     // substring方法截取字串,可以指定截取的起始位置和終止位置     System.out.print("sb.substring(4,9)=" + sb.substring(4, 9));   }    /**    * 擷取字串中某個位置的字元    */   public static void testCharAtStr() {     StringBuffer sb = new StringBuffer("This is a StringBuffer");     System.out.println(sb.charAt(sb.length() - 1));   }    /**    * 添加各種類型的資料到字串的尾部    */   public static void testAppend() {     StringBuffer sb = new StringBuffer("This is a StringBuffer!");     sb.append(1.23f);     System.out.println(sb.toString());   }    /**    * 刪除字串中的資料    */   public static void testDelete() {     StringBuffer sb = new StringBuffer("This is a StringBuffer!");     sb.delete(0, 5);     sb.deleteCharAt(sb.length() - 1);     System.out.println(sb.toString());   }    /**    * 向字串中插入各種類型的資料    */   public static void testInsert() {     StringBuffer sb = new StringBuffer("This is a StringBuffer!");     // 能夠在指定位置插入字元、字元數組、字串以及各種數字和布爾值     sb.insert(2, 'W');     sb.insert(3, new char[] { 'A', 'B', 'C' });     sb.insert(8, "abc");     sb.insert(2, 3);     sb.insert(3, 2.3f);     sb.insert(6, 3.75d);     sb.insert(5, 9843L);     sb.insert(2, true);     System.out.println("testInsert: " + sb.toString());   }    /**    * 替換字串中的某些字元    */   public static void testReplace() {     StringBuffer sb = new StringBuffer("This is a StringBuffer!");     // 將字串中某段字元替換成另一個字串     sb.replace(10, sb.length(), "Integer");     System.out.println("testReplace: " + sb.toString());   }    /**    * 將字串倒序    */   public static void reverseStr() {     StringBuffer sb = new StringBuffer("This is a StringBuffer!");     System.out.println(sb.reverse()); // reverse方法將字串倒序   } } 

小結:
StringBuffer不是不變類,在修改字串內容時,不會建立新的對象,因此,它比String類更適合修改字串;
StringBuffer類沒有提供同String一樣的toCharArray方法;
StringBuffer類的replace方法同String類的replace方法不同,它的replace方法有三個參數,第一個參數指定被替換子串的起始位置,第二個參數指定被替換子串的終止位置,第三個參數指定新子串。

以上就是本文的全部內容,希望對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.