java中的StringBuffer的用法

时间:2023-01-04 22:14:31

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);

}
}