1. 程式人生 > >Java StringBuffer類

Java StringBuffer類

pen let style () ati bsp clas turn col

 1 package demo03;
 2 
 3 public class StringBufferDemo {
 4     public static void main(String[] args) {
 5         func();
 6     }
 7     
 8     public static void func() {
 9         StringBuffer buffer = new StringBuffer();
10         buffer.append("abcd").append(1);
11         System.out.println(buffer);    //
abcd1 12 13 buffer.delete(1, 3); 14 System.out.println(buffer); //ad1 15 16 buffer.insert(1, "123"); 17 System.out.println(buffer); //a123d1 18 19 buffer.replace(1, 4, "jdk"); 20 System.out.println(buffer); //ajdkd1 21 22
buffer.reverse(); 23 System.out.println(buffer); //1dkdja 24 25 String s = buffer.toString(); 26 System.out.println(s); //1dkdja 27 28 } 29 }

demo

 1 package demo03;
 2 
 3 public class StringBufferTest {
 4     //{1,2,3,4,5} => [1,2,3,4,5]
5 public static void main(String[] args) { 6 int[] arr = {1,2,3,4,5}; 7 System.out.println(toString(arr)); 8 } 9 10 public static String toString(int[] arr) { 11 StringBuffer buffer = new StringBuffer(); 12 buffer.append("["); 13 for (int i = 0; i < arr.length; i++) { 14 if (i == arr.length - 1) { 15 buffer.append(arr[i]).append("]"); 16 } else { 17 buffer.append(arr[i]).append(","); 18 } 19 } 20 21 return buffer.toString(); 22 } 23 24 }

Java StringBuffer類