天天看点

Java StringBulider类、String 和StringBulider类之间的转换

​​129_StringBuilder添加和反转_哔哩哔哩_bilibili​​

Java StringBulider类、String 和StringBulider类之间的转换
public class Main {
    public static void main(String[] args) {
        StringBuilder myPoem = new StringBuilder("水是眼波横,山是聚眉峰。");
        System.out.println("System.identityHashCode(myPoem) = " + System.identityHashCode(myPoem));
        
        Object poem01 = "欲问行人去那边?眉眼盈盈处。";
        System.out.println("System.identityHashCode(poem01) = " + System.identityHashCode(poem01));

        StringBuilder wangguanPoem = myPoem.append(poem01);
        System.out.println("System.identityHashCode(wangguanPoem) = " + System.identityHashCode(wangguanPoem));
        wangguanPoem.append("才始送春归,又送群归去。");
        System.out.println("System.identityHashCode(wangguanPoem) = " + System.identityHashCode(wangguanPoem));
        wangguanPoem.append("若到江南赶上春,").append("千万和春住。").append("宋代 " +
                "王观").append("卜算子-送鲍浩然之浙东");  //链式编程
        System.out.println("System.identityHashCode(wangguanPoem) = " + System.identityHashCode(wangguanPoem));

        System.out.println("myPoem: " + myPoem);
        System.out.println("--------------------------------------");
        System.out.println( "wangguanPoem: " + wangguanPoem);

        System.out.println(wangguanPoem.reverse());
    }
}      
Java StringBulider类、String 和StringBulider类之间的转换

String 和StringBulider类之间的转换

Java StringBulider类、String 和StringBulider类之间的转换
public class Main {
    public static void main(String[] args) {
        StringBuilder myPoem = new StringBuilder("水是眼波横,山是聚眉峰。");
        String poem01 = "欲问行人去那边?眉眼盈盈处。";

        String wangguanPoem = myPoem.toString();
        wangguanPoem += poem01;
        System.out.println(wangguanPoem);

        String poem02 = "才始送春归,又送君归去。";
        StringBuilder poem03 = new StringBuilder(poem02);
        System.out.println(myPoem.append(poem02));
        
    }
}