zoukankan      html  css  js  c++  java
  • 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               
    }                                                                              
  • 相关阅读:
    MySQL客户端管理
    Windows10安装Pytorch环境要点
    使用ssh加密github通信
    JVM 对象状态判断01
    并发之AbstractQueuedLongSynchronize----AQS
    关于CountDownLatch控制线程的执行顺序
    关于线程执行顺序的问题
    并发之Striped64(l累加器)
    并发之线程以及线程的中断状态
    1 JPA入门----项目搭建以及CRUD
  • 原文地址:https://www.cnblogs.com/ericguoxiaofeng/p/8094899.html
Copyright © 2011-2022 走看看