3,提高安全性。
class StringBufferDemo { public static void main(String[] args) { //method_update(); StringBuilder sb = new StringBuilder("abcdef"); char[] chs = new char[6]; sb.getChars(1,4,chs,1);//将 for(int x=0; x<chs.length; x++) { sop("chs["+x+"]="+chs[x]+";"); } draw(3,6); draw(8,9); // StringBuilder sb1 = new StringBuilder(); // sb1.append(new Demo()).append(new Demo()); // sop("sb1="+sb1); } public static void method_update() { StringBuffer sb = new StringBuffer("abcde"); // sb.replace(1,4,"java"); sb.setCharAt(2,'k'); sop(sb.toString()); } public static void method_del() { StringBuffer sb = new StringBuffer("abcde"); // sb.delete(1,3); //清空缓冲区。 //sb.delete(0,sb.length()); //sb.delete(2,3); sb.deleteCharAt(2); sop(sb.toString()); } public static void method_add() { StringBuffer sb = new StringBuffer(); //sb.append("abc").append(true).append(34); // StringBuffer sb1 = sb.append(34); // sop("sb==sb1:"+(sb==sb1));//true sb.insert(1,"qq"); sop(sb.toString());//abctrue34 //sop(sb1.toString()); } public static void sop(String str) { System.out.println(str); } public static void draw(int row,int col) { StringBuilder sb = new StringBuilder(); for(int x=0; x<row; x++) { for(int y=0; y<col; y++) { sb.append("*"); } sb.append("\r\n"); } sop(sb.toString()); } }
————摘自《毕向东25天》