java学习之路----常用类库---StringBuffer类

1.StringBuffer类
      在以前我们学过String类,它是一个不可改变的类,一旦声明,这不可改变。即使改变也是改变的String的引用地址

          如果想要经常改变字符串,那就要用StringBuffer类

          我们知道String类是用"+"来连接字符串的,但是在StringBuffer确实用append方法来连接的字符串

     下面就是一些StringBuffer常用的方法


          
      1.字符串的连接

           public   class  StringBufferDemo {
              
       public   static   void  main(String[] args) {
          StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
          stringBuffer.append(  " world"  ).append( "!!!!"  );
          System.  out  .println(stringBuffer);
     }
}

结果:
hello world!!!!


2.在任意位置为StringBuffer添加内容

      public   class  StringBufferDemo {
              
       public   static   void  main(String[] args) {
          StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
          stringBuffer.append(  " world"  ).append( "!!!!"  );
          System.  out  .println(stringBuffer);
          
          stringBuffer.insert(0,  "like say"  );//在初始位置(第一个位置)插入
          System.  out  .println(stringBuffer);
          stringBuffer.insert(stringBuffer.length(),  "OK"  );//在最后插入
          System.  out  .println(stringBuffer);
     }
}
结果:
hello world!!!!
like sayhello world!!!!
like sayhello world!!!!OK

3.字符串的反转操作

      public   class  StringBufferDemo {
              
       public   static   void  main(String[] args) {
          StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
           stringBuffer.append(  " world" );
          System.  out  .println(stringBuffer);
          
     String s= stringBuffer.reverse().toString();  //反转,在转为字符串
          System.  out  .println(s);
     }
}
结果:
hello world
dlrow olleh


4.替换指定范围的内容

public   class  StringBufferDemo {
              
       public   static   void  main(String[] args) {
           StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
          stringBuffer.append(  " world"  );
          System.  out  .println(stringBuffer);
          stringBuffer.replace(6, 11,  "like"  ); //替换6到11的字符串
          System.  out  .println(stringBuffer);
     }
}

结果:
hello world
hello like

5.截取字符串
           public   class  StringBufferDemo {
              
       public   static   void  main(String [] args) {
          StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
          stringBuffer.append(  " world"  );
          System.  out  .println(stringBuffer);
          stringBuffer.replace(6, 11,  "like"  ); //替换6到11的字符串
          System.  out  .println(stringBuffer);
           String s= stringBuffer.substring(6,10);  //截取6到10的字符串
          System.  out  .println(s);
     }
}

结果:
hello world
hello like
like

6.删除指定位置的字符串

public   class  StringBufferDemo {
              
       public   static   void  main(String[] args) {
          StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
          stringBuffer.append(  " world"  );
          System.  out  .println(stringBuffer);
          stringBuffer.replace(6, 11,  "like"  ); //替换6到11的字符串
          System.  out  .println(stringBuffer);
          String s= stringBuffer.substring(6,10);  //截取6到10的字符串
          System.  out  .println(s);
          System.  out  .println(stringBuffer);
          stringBuffer.delete(6, 10);  //删除6到10的字符串
          
          System.  out  .println(stringBuffer);
          
     }
}
结果:
hello world
hello like
like
hello like
hello

7.查找指定的内容是否存在

       public   static   void  main(String[] args) {
          StringBuffer stringBuffer=  new  StringBuffer();
          stringBuffer.append(  "hello"  );
          stringBuffer.append(  " world"  );
           System.  out .println(stringBuffer);
          stringBuffer.replace(6, 11,  "like"  ); //替换6到11的字符串
          System.  out  .println(stringBuffer);
          String s= stringBuffer.substring(6,10);  //截取6到10的字符串
          System.  out  .println(s);
          System.  out  .println(stringBuffer);
          stringBuffer.delete(6, 10);  //删除6到10的字符串
          
          System.  out  .println(stringBuffer);
          System.  out  .println(stringBuffer.indexOf( "hello"  )); //查找指定的字符串是否存在
          System.  out  .println(stringBuffer.indexOf( "like"  ));
          
     }
}

结果:
hello world
hello like
like
hello like
hello
0
-1

分析结果:0表示查找的字符串的起始位置是0开始的
-1表示没有找到


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值