1 package com.jdk7.chapter5; 2 /** 3 * 4 * @author Administrator 5 * 6 */ 7 public class StringBufferTest { 8 9 public static void findStr(StringBuffer sb, String dest){ 10 System.out.println("指定匹配开始的位置"+"("+sb+")"+".indexOf("+dest+")>"+sb.indexOf(dest)); 11 System.out.println("指定匹配开始的位置"+"("+sb+")"+".indexOf("+dest+", 4)>"+sb.indexOf(dest, 4)); 12 System.out.println("指定匹配结束的位置"+"("+sb+")"+".lastIndexOf("+dest+")>"+sb.lastIndexOf(dest)); 13 System.out.println("指定匹配结束的位置"+"("+sb+")"+".lastIndexOf("+dest+", 4)>"+sb.lastIndexOf(dest, 2)); 14 } 15 16 public static void subStr(StringBuffer sb, int n, int m){ 17 System.out.println("从位置"+n+"开始截取"+"("+sb+").substring("+n+")>"+sb.substring(n)); 18 System.out.println("从位置"+n+"开始到"+m+"结束截取"+"("+sb+").substring("+n+", "+m+")>"+sb.substring(n, m)); 19 } 20 21 public static void charAt(StringBuffer sb, int n){ 22 System.out.println("返回位置"+n+"的字符("+sb+").charAt("+n+")"+sb.charAt(n)); 23 } 24 25 public static void append(){ 26 StringBuffer sb = new StringBuffer(); 27 sb.append("Hello").append(" ").append(new char[]{‘w‘,‘o‘,‘r‘,‘l‘,‘d‘,‘!‘}).append(3).append(true).append(6.44d) 28 .append(78.445f); 29 System.out.println("append>"+sb.toString()); 30 } 31 32 public static void insert(StringBuffer sb, int n){ 33 System.out.println("("+sb+").insert>"+sb.insert(n, false).insert(n, ‘b‘).insert(n, new char[]{‘w‘,‘o‘,‘r‘,‘l‘,‘d‘,‘!‘}) 34 .insert(n, 33.55d).insert(n, 7768.22f).insert(n, 66)); 35 } 36 37 public static void delete(StringBuffer sb, int start, int end){ 38 System.out.println("("+sb+").delete("+start+", "+end+")>"+sb.delete(start, end)); 39 System.out.println("("+sb+").deleteCharAt("+start+")>"+sb.deleteCharAt(start)); 40 } 41 42 public static void replace(StringBuffer sb, int start, int end, String str){ 43 System.out.println("("+sb+").replace("+start+", "+end+", "+str+")>"+sb.replace(start, end, str)); 44 } 45 46 public static void reverse(StringBuffer sb){ 47 System.out.println("("+sb+").reverse()>"+sb.reverse()); 48 System.out.println("("+sb+").capacity()"+sb.capacity()); 49 } 50 public static void main(String[] args) { 51 StringBuffer sb = new StringBuffer("This is an object!"); 52 StringBufferTest.findStr(sb, "is"); 53 StringBufferTest.subStr(sb, 2, 10); 54 StringBufferTest.charAt(sb, 6); 55 StringBufferTest.append(); 56 StringBufferTest.insert(sb, 10); 57 StringBufferTest.delete(sb, 2, 7); 58 StringBufferTest.replace(sb, 2, 7, "hello"); 59 StringBufferTest.reverse(sb); 60 } 61 } 62 63 执行结果: 64 指定匹配开始的位置(This is an object!).indexOf(is)>2 65 指定匹配开始的位置(This is an object!).indexOf(is, 4)>5 66 指定匹配结束的位置(This is an object!).lastIndexOf(is)>5 67 指定匹配结束的位置(This is an object!).lastIndexOf(is, 4)>2 68 从位置2开始截取(This is an object!).substring(2)>is is an object! 69 从位置2开始到10结束截取(This is an object!).substring(2, 10)>is is an 70 返回位置6的字符(This is an object!).charAt(6)s 71 append>Hello world!3true6.4478.445 72 (This is an object!).insert>This is an667768.2233.55world!bfalse object! 73 (This is an667768.2233.55world!bfalse object!).delete(2, 7)>Th an667768.2233.55world!bfalse object! 74 (Th an667768.2233.55world!bfalse object!).deleteCharAt(2)>Than667768.2233.55world!bfalse object! 75 (Than667768.2233.55world!bfalse object!).replace(2, 7, hello)>Thhello768.2233.55world!bfalse object! 76 (Thhello768.2233.55world!bfalse object!).reverse()>!tcejbo eslafb!dlrow55.3322.867ollehhT 77 (!tcejbo eslafb!dlrow55.3322.867ollehhT).capacity()70