String builder 的使用方法:
1.append方法
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("Hello,");
sb.append("World!");
System.out.println(sb);
}
输出为:Hello,World
直接拼接
2.insert方法
public static void main(String[] args) {
int pay = 10;
StringBuilder sb = new StringBuilder("Hello world!");
sb.insert(6,"nice ");
System.out.println(sb);
}
输出为:Hello nice world!
在下标6处插入字符串
3.delete方法
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hell12345");
sb.delete(4,7);
System.out.println(sb);
}
输出为:Hell45
删除下标5-7的字符;删除包括5 不包括7。
4.replace方法
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("!!!@@@###");
sb.replace(0,4,"?");
System.out.println(sb);
}
输出:?@@###
将下标0-4的字符替代为’?'字符。(包括小标0,不包括下标4)