StringBuffer

 
private static void deleteTest() {                               
    StringBuffer sb = new StringBuffer("guoxiaofeng");           
    sb.delete(3, 7);                                             
    System.out.println(sb.toString());//guofeng                  
    sb.deleteCharAt(3);                                          
    System.out.println(sb.toString());//guoeng                   
    //清空缓冲区的方法                                                   
    System.out.println("清空" + sb.delete(0, sb.length()));//清空    
}                                                                

 

private static void insertTest() {                    
    StringBuffer sb = new StringBuffer("guoxiaofeng");
    sb.insert(3, "hei");                              
    System.out.println(sb.toString());//guoheixiaofeng
}                                                     

 

/*                                          
 * 因为全部都在缓冲区,视为同一个                          
 * 下列结果全部打印出: trueheima100                  
 */                                         
private static void appendTest() {          
    StringBuffer sb1 = new StringBuffer();  
    StringBuffer sb2 = sb1.append(true);    
    StringBuffer sb3 = sb1.append("heima"); 
    StringBuffer sb4 = sb1.append(100);     
                                            
    System.out.println(sb1.toString());     
    System.out.println(sb2.toString());     
    System.out.println(sb3.toString());     
    System.out.println(sb4.toString());     
}                                           

 

/*                                         
 * 因为全部都在缓冲区,视为同一个                         
 * 下列结果全部打印出: heima100                     
 */                                        
private static void appendTest() {         
    StringBuffer sb1 = new StringBuffer(); 
    StringBuffer sb2 = sb1.append(true);   
    sb2.delete(0, sb2.length());//清空缓存     
    StringBuffer sb3 = sb1.append("heima");
    StringBuffer sb4 = sb1.append(100);    
                                           
    System.out.println(sb1.toString());    
    System.out.println(sb2.toString());    
    System.out.println(sb3.toString());    
    System.out.println(sb4.toString());    
}                                          

 构造方法

public StringBuffer() {
     super(16);
}

public StringBuffer(int capacity) {
    super(capacity);               
}  

public StringBuffer(String str) {
    super(str.length() + 16);    
    append(str);                 
}        

public StringBuffer(CharSequence seq) {
    this(seq.length() + 16);           
    append(seq);                       
}                                                                                              

 

private static void constructorTest() {                                        
    StringBuffer sb = new StringBuffer();                                      
    System.out.println(sb.length());  //0                                      
    System.out.println(sb.capacity());//16                                     
                                                                               
    StringBuffer sb2 = new StringBuffer(10);                                   
    System.out.println(sb2.length());  //0                                     
    System.out.println(sb2.capacity());//10                                    
                                                                               
    StringBuffer sb3 = new StringBuffer("eric");                               
    System.out.println(sb3.length());    //4                                   
    System.out.println(sb3.capacity());  //字符串的长度加上初始容量16。打印出:20               
}                                                                              

 

posted @ 2017-12-23 23:54  天马行空郭  阅读(116)  评论(0编辑  收藏  举报