129_StringBuilder添加和反转_哔哩哔哩_bilibili
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0gTMx81dsQWZ4lmZf1GLlpXazVmcvwFciV2dsQXYtJ3bm9CX9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCMy81dvRWYoNHLwEzX5xCMx8FesU2cfdGLwMzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5iMzYDO0cDNjdjM3UmMzITMzYzXzIDN0ADMzAzLclDMyIDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLyM3Lc9CX6MHc0RHaiojIsJye.png)
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());
}
}
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));
}
}