父类:java.lang.Object
java.lang.StringBuffer1.StringBuffer构造方法1>StringBuffer() 构造一个不带字符的字符缓冲区,其初始容量为16个字符2>StringBuffer(CharSequence seq) 构造一个字符串缓冲区包含与接口CharSequence相同的字符 接口CharSequence的实现类包括String类/StringBuffer类/StringBuilder类 因此StringBuffer(String value)是可以的;3>StringBuffer(int capacity) 指定初始容量的字符串缓冲区4>StringBuffer(String str)//见2>
2.StringBuffer特有方法1>public StringBuffer append(int x)//(char value)//(boolean value)
1.StringBuffer leo9257 = new StringBuffer("nanjing.");
2.System.out.println(leo9257);//nanjing.
3.leo9257.append("guiyang");
4.System.out.println(leo9257);//nanjing.guiyang
5.leo9257.append(true).append('@').append(2009);
6.System.out.println(leo9257);//nanjing.guiyangtrue@2009
2>public StringBuffer insert(int off,String str)
1.StringBuffer leo9257 = new StringBuffer("nanjing.");
2.leo9257.insert(6, "guiyang");//nanjinguiyangg.在索引出插入
3.leo9257.insert(8, "guiyang");//nanjing.guiyang
3>public StringBuffer delete(int start,int end)
1.StringBuffer leo9257 = new StringBuffer("nanjing.guiyang2009-201*");
2.leo9257.delete(19,leo9257.length());//nanjing.guiyang2009
4>public StringBuffer reserve()
1.StringBuffer leo9257 = new StringBuffer("nanjing.guiyang2009-201*");
2.leo9257.reverse();//*102-9002gnayiug.gnijnan
5>public StringBuffer replace(int Start,int end,String str)
1.StringBuffer leo9257 = new StringBuffer("nanjing...guiyang2009");
2.leo9257.replace(7, 9, "pukou");//nanjingpukou.guiyang2009
6.eg
/*
*
*将一句话的首字母都转为大写
*leo9257 is the man who stayed in nanjing for nearly nine years.
*/
public class UpperCaseDeom {
public static void main(String[] args) {
String s = "leo9257 is the man who stayed in Nanjing for nearly 9 years.";
StringBuffer ns = new StringBuffer();
String[] words = s.split(" ");
for (int i = 0; i < words.length; i++) {
String s0 = words[i].substring(0,1).toUpperCase()+words[i].substring(1);
ns.append(s0+" ");
}
System.out.println(ns);
}
}
View Code