StringBuffer类的方法

public class Page116 {

    /**
     * StringBuffer类的练习
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        StringBuffer sb1=new StringBuffer();
        StringBuffer sb2=new StringBuffer(30);
        StringBuffer sb3=new StringBuffer("hello");
        System.out.println(sb1.capacity());//返回Stringbuffer对象的当前容量==16,表示缓冲区空间的大小
        System.out.println(sb2.capacity());//==30
System.out.println(sb3.capacity());==16+5=21 System.out.println(sb3.length());
//返回Stringbuffer对象的长度,5 System.out.println("请输入学生名单:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringBuffer stuName=new StringBuffer(); for(int i=0;i<5;i++){ String name=br.readLine(); stuName.append(name+" "); if(i==4){ System.out.println("录入完毕"); } } System.out.println("录入的学生有: "+stuName); System.out.println(); System.out.println("请输入你要提交的java文件名称"); BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); String fileName=br1.readLine(); int chIndex=fileName.lastIndexOf('。'); int enIndex=fileName.lastIndexOf('.'); System.out.println(enIndex); if(enIndex!=-1&&fileName.substring(enIndex+1, fileName.length()).equals("java")){ System.out.println("提交成功"); }else if(chIndex!=-1&&fileName.substring(chIndex+1, fileName.length()).equals("java")){ StringBuffer file=new StringBuffer(fileName); file.setCharAt(chIndex, '.'); System.out.println("您的书写有误,已改正,提交成功"); }else{ System.out.println("您的格式有问题"); } StringBuffer sb5=new StringBuffer("hello"); sb3.append("java");//字符串拼接 System.out.println(sb5); System.out.println(sb5.deleteCharAt(3));//删除指定位置的字符 StringBuffer sb4=sb3.reverse();//字符串反转 System.out.println(sb3); System.out.println(sb4); } }

结果显示:

16
30
21
5
请输入学生名单:
赵
孙
李
王
牛
录入完毕
录入的学生有:
赵    孙    李    王    牛    

请输入你要提交的java文件名称
abc.java
3
提交成功
hello
helo
avajolleh
avajolleh
原文地址:https://www.cnblogs.com/tjlgdx/p/5994448.html