public class StringBuffer3 {
public static void main(String[] args) {
//demo1();
demo2();
}
public static void demo2() {
int arr[]={1,2,3,4};
StringBuffer sb=new StringBuffer();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if(i!=arr.length-1){
sb.append(arr[i]+", ");
}
else{
sb.append(arr[i]+"]");
}
}
System.out.println(sb.toString());
}
private static void demo1() {
StringBuffer s=new StringBuffer("heima");
s.replace(0, 3, "bai");//替换
System.out.println(s);
System.out.println(s.insert(1, "hh"));
System.out.println(s.reverse());//反转
System.out.println(s.substring(2));//返回值是String截取
System.out.println(s.substring(2, 4));
s.append("chuan").append("zhi");//添加
System.out.println(s);
System.out.println(s.delete(0, 2));//删除
System.out.println(s.deleteCharAt(0));
String str="heima";
StringBuffer sb=new StringBuffer(str);//string 转换成StringBuffer
System.out.println(sb);
// StringBuffer转换成string
System.out.println(sb.toString());
String sss=new String(sb);
System.out.println(sss);
String ssss=sb.substring(0);
System.out.println(ssss);
Student stu=new Student();
System.out.println(sb.insert(2,stu));
}
}
|
|