java中的StringBuffer的用法

package Main;
import java.util.*;
import java.io.*;


class Main{
public static void cout(Object s) {
System.out.println(s);
}
// public static void cout(String s) {
// System.out.println(s);
// }
// public static void cout(char s) {
// System.out.println(s);
// }
// public static void cout(int s) {
// System.out.println(s);
// }
// public static void cout(boolean s) {
// System.out.println(s);
// }
// public static void cout(double s) {
// System.out.println(s);
// }
// public static void cout(StringBuffer s) {
// System.out.println(s);
// }
public static void main(String args[]) {
StringBuffer s=new StringBuffer("012345");
s.append(9);cout(s);//0123459//add others into the back of the StringBuffer;
s.deleteCharAt(6);cout(s);//012345//delete the charactor whose index is 6;
cout(s.capacity());//22//return the space capacity of the StringBuffer;
cout(s.length());//6//retrun the length of the StringBuffer;
char []a=new char [10];
s.getChars(1,4,a,2);//void getChars(int srcBegin, int srcEnd,char[]dst, int dstBegin);
//be care for the srcEnd is exlusived;
for(char i:a)System.out.print(i+" ");cout("");//    1 2 3//use the foreach statement to output;
cout(a[0]);//remind of the a[0] is null;Due to the begin of the array a is not assigned;
s.setCharAt(2, '9');cout(s);//019345//void setCharAt(int index,char ch);//reset the charactor at 2 to '9';
s.setCharAt(2, '2');cout(s);//012345
s.setLength(5);cout(s);//01234//void setLength(int newLength);if the newlength is much shorter than the original,
//the excess data will be erased;
s.delete(2, 4);cout(s);//014//erase the charactor from the start to the end-1;
s.insert(2, "23");cout(s);//01234//insert(int index, Object b);insert Object before the index;
s.append("5");cout(s);//012345//insert the string "5" into the back of it;
s.replace(1,3,"789");cout(s);//0789345//void replace(int start, int end, string str);replace the string with str;
s.replace(1,5,"123");cout(s);//012345//;
s.reverse();cout(s);//reverse the string as replacing the charactor between the front and back;
cout(s.toString());
s.reverse();cout(s);

}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值