1 // Test StringBuffer.replace(), reverse(), insert(String), append(String), 2 // and delete(). 3 4 public class StringBuffer_1 5 { main(String args[])6 public static void main(String args[]) 7 { 8 StringBuffer sb = new StringBuffer("45"); 9 sb.insert(0, "123"); 10 sb.append("89"); 11 sb.insert(5, "6"); 12 sb.insert(6, '7'); 13 System.out.println (sb); 14 15 sb.delete (3, 99); 16 17 String foo = sb.toString(); 18 19 System.out.println (foo); 20 sb.reverse(); 21 System.out.println (foo); 22 23 System.out.println (sb); 24 sb = new StringBuffer("1234"); 25 System.out.println(sb.reverse()); 26 27 sb = new StringBuffer("123456789"); 28 sb.append ("0"); 29 System.out.println(sb); 30 31 sb.replace (2, 99, "foo"); 32 System.out.println (sb); 33 34 sb = new StringBuffer("123456789"); 35 sb.replace (1, 1, "XX"); 36 System.out.println (sb); 37 38 sb = new StringBuffer("123456789"); 39 sb.replace (0, 2, "XX"); 40 System.out.println (sb); 41 42 sb = new StringBuffer("123456789"); 43 sb.replace (5, 9, "54321"); 44 System.out.println (sb); 45 46 sb = new StringBuffer("123456789"); 47 48 sb.delete (1,4); 49 System.out.println (sb); 50 51 // Test bounds checks 52 try 53 { 54 sb.insert (-2, "x"); 55 } 56 catch (StringIndexOutOfBoundsException x) 57 { 58 System.out.println (x.getClass()); 59 } 60 61 try 62 { 63 sb.insert (96, "x"); 64 } 65 catch (StringIndexOutOfBoundsException x) 66 { 67 System.out.println (x.getClass()); 68 } 69 70 try 71 { 72 sb.delete (-2, 2); 73 } 74 catch (StringIndexOutOfBoundsException x) 75 { 76 System.out.println (x.getClass()); 77 } 78 79 try 80 { 81 sb.delete (96, 418); 82 } 83 catch (StringIndexOutOfBoundsException x) 84 { 85 System.out.println (x.getClass()); 86 } 87 88 try 89 { 90 sb.delete (4, 2); 91 } 92 catch (StringIndexOutOfBoundsException x) 93 { 94 System.out.println (x.getClass()); 95 } 96 97 try 98 { 99 sb.replace (-2, 2, "54321"); 100 } 101 catch (StringIndexOutOfBoundsException x) 102 { 103 System.out.println (x.getClass()); 104 } 105 106 try 107 { 108 sb.replace (4, 2, "54321"); 109 } 110 catch (StringIndexOutOfBoundsException x) 111 { 112 System.out.println (x.getClass()); 113 } 114 115 try 116 { 117 sb.replace (12, 18, "54321"); 118 } 119 catch (StringIndexOutOfBoundsException x) 120 { 121 System.out.println (x.getClass()); 122 } 123 } 124 } 125