String StringBuffer StringBuilder类

柔情只为你懂 2022-06-14 06:10 368阅读 0赞

String类是由final修饰的,使用值是不可变的,在进行大量修改拼接字符串时,不应该使用String

StringBuffer

长度可变,线程安全

相关常用方法

  1. public class M1 {
  2. public static void main(String[] args) {
  3. StringBuffer str = new StringBuffer("abcdefssss");
  4. /**
  5. * append 向末尾追加,可以最加字符串,字符数组,字符,布尔型,int,double等
  6. */
  7. str.deleteCharAt(1); //移除指定索引
  8. str.delete(0, 2); //移除范围内的
  9. System.out.println(str.indexOf("f")); //返回第一次出现的序列,
  10. System.out.println(str.indexOf("s",5));//从指定索引处开始找
  11. /**
  12. * insert 向指定位置插入
  13. */
  14. str.insert(1, "xxx"); //指定索引位置上插入
  15. System.out.println(str);
  16. }
  17. }

StringBuilder

速度比StringBuffer快,但非线程安全,在不用线程的情况下推荐使用StringBuilder

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读