java.lang.StringBuffer 的常用接口方法
1 : append(char c)
将 char参数的字符串表示附加到此序列。
public class String01Test {
public static void main(String[] args) {
StringBuffer thing = new StringBuffer("hello");
String str = " world!";
thing.append(str);
System.out.println(thing);
}
}
运行结果
hello world!
2 : insert(int up, char c)
在此序列中插入 char参数的字符串表示形式。 (第一个参数是表示从该位置后进行插入)
public class String01Test {
public static void main(String[] args) {
StringBuffer thing = new StringBuffer("hello");
String str = " world!";
thing.insert(0,str);
System.out.println(thing);
}
}
运行结果
world!hello
3 : lenth();
返回长度(字符数)。
public class String01Test {
public static void main(String[] args) {
StringBuffer thing = new StringBuffer("hello");
int num = thing.length();
System.out.println(num);
}
}
运行结果
5
4 : reverse()
导致该字符序列被序列的相反代替。
public class String01Test {
public static void main(String[] args) {
StringBuffer thing = new StringBuffer("hello");
thing.reverse();
System.out.println(thing);
}
}
运行结果
olleh
5 : delete(int start,int end)
删除此序列的子字符串中的字符 从start到end序列的字符串会被删除
public class String01Test {
public static void main(String[] args) {
StringBuffer thing = new StringBuffer("hello");
thing.delete(0,3);
System.out.println(thing);
}
}
运行结果
lo
以上为StringBuffer类常用的接口方法