StringBuilder是一个容器,是java已经写好的类,其方法可以直接用
1.public StringBuilder append(任意类型):添加数据,并返回对象本身
package jichugainian;
public class test5 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append(1);
sb.append("acd");
sb.append(3.34);
System.out.println(sb);
}
}
2.public StringBuilder reverse():反转容器中的内容
package jichugainian;
public class test5 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abc");
sb.reverse();
System.out.println(sb);
}
}
3.public int length():返回长度
package jichugainian;
public class test5 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abc");
System.out.println(sb.length());
}
}
4.public String toString():把StringBuilder转换为String
package jichugainian;
public class test5 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("abc");
String str = sb.toString();
System.out.println(str);
}
}
StringBuilder用于字符串的拼接和字符串的反转