今天敲代码了吗?   

String和StringBuffer转换

/*String 转为StringBuffer*/
public class StringBufferDemo {
    public static void main(String[] args) {
        //String --> StringBuffer
        String string = "Hello";
        
        //构造方法转换
        StringBuffer buffer = new StringBuffer(string);
        System.out.println(buffer);
    
        //append()方法
        StringBuffer buffer2 = new StringBuffer();
        buffer2.append(string);
        System.out.println("buffer2:"+buffer2);
    }
}
/*StringBuffer转String*/
public class StringBufferDemo {
    public static void main(String[] args) {
        //StringBuffer -->  String 
        
        StringBuffer buffer = new StringBuffer("Java");
        
        //构造方法转换
        String string = new String(buffer);
        System.out.println("string:"+string);
        
        //toSting()方法
        //通过toString方法
        String  string2 = buffer.toString();
        System.out.println("string2 : "+string2);
    }
}

 

posted on 2020-11-28 19:07  今天学算法了吗?  阅读(206)  评论(0编辑  收藏  举报

导航