1.StringBuilder和StringBuffer用法一致,但是StringBuilder线程不安全,速度较快
2.StringBuilder和String的转换
public class StringBuilderTest1 {
public static void main(String[] args) {
StringBuilder strBui = new StringBuilder();
strBui.append("hello").append("world");
String str = strBui.toString();
System.out.println("StringBuilder转换为String输出:"+str);
StringBuilder strBui1 = new StringBuilder(str);
System.out.println("String转换为StringBuilder输出:"+strBui1);
}
}
//StringBuilder转换为String输出:helloworld
//String转换为StringBuilder输出:helloworld
3.StringBuilder的拼接,反转
public class StringBuilderTest2 {
public static void main(String[] args) {
StringBuilder strBui = new StringBuilder();
strBui.append(hello).append("abc").append(123); //拼接
System.out.println(strBui);
System.out.println(strBui.reverse()); //反转
}
}
// helloabc123
//321cbaolleh
4.字符串是否对称
public class StringBuilderTest3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.nextLine();
boolean b = isSymmetry(str);
System.out.println("是否对称:"+b);
}
public static boolean isSymmetry(String str) {
StringBuilder strBui = new StringBuilder(str);
strBui.reverse();
String strRes = strBui.toString();
return strRes.equals(str);
}
}
//请输入一个字符串:abc
//是否对称:false
5.明天学习内容:字符串的编码
.