常用方法:
public class Main {
public static void main(String[] args) {
StringBuilder builder=new StringBuilder();
//所有修改都针对自身,并没有一个新的builder返回
//字符串连接
builder.append("Hello");
builder.append(" World");
System.out.println(builder.toString());
//定位字符串
System.out.println(builder.indexOf("W"));
//字符串替换
builder.replace(1,4,"ooo");
System.out.println(builder.toString());
//字符串插入
builder.insert(1,"ello");
System.out.println(builder.toString());
//字符串删除
builder.delete(4,8);
System.out.println(builder.toString());
//定位
System.out.println(builder.lastIndexOf("o"));
//长度
System.out.println(builder.length());
//字符串反转
builder.reverse();
System.out.println(builder.toString());
}
}
运行结果: