0%

可变字符串的插入删除和替换

1
2
3
4
5
6
7
8
9
10
11
12
// 2.22 字符串插入、删除、替换
// 插入
StringBuffer str = new StringBuffer("Today is a good day!");
str.insert(11," very ");
System.out.println(str);// Today is a very good day!
//删除和替换
StringBuffer str1 = new StringBuffer("Today is a good day!");
StringBuffer str2 = new StringBuffer("Today is a good day!");
StringBuffer str3 = new StringBuffer("Today is a good day!");
System.out.println(str1.delete(0, 5));// is a good day!
System.out.println(str2.delete(0, 5));// is a good day!
System.out.println(str3.replace(0,5,"Tomorrow "));//Tomorrow is a good day!