java之StringBuilder

在Java中,StringBuilder类是一个可变对象,用于处理字符串。它提供了许多方法来操作字符串,以下是其中一些常用的方法:

  1. append(String str) 将指定的字符串追加到此字符串构建器的末尾。也可以是单个字符、整数、浮点数、
  2. StringBuilder sb = new StringBuilder("Hello");  
    sb.append(" World"); // appends " World" to the end of "Hello"

  3. delete(int start, int end) 从此字符串构建器中删除从start到end的子字符串。
  4. StringBuilder sb = new StringBuilder("Hello World");  
    sb.delete(5, 10); // deletes the characters from index 5 to index 10 (inclusive), 得到Hellold

  5. deleteCharAt(int index) 从此字符串构建器中删除指定位置的字符。
  6. StringBuilder sb = new StringBuilder("Hello World");  
    sb.deleteCharAt(7); // deletes the character at index 7,得到Hello, orld

  7. insert(int index, String str) 将指定的字符串插入到此字符串构建器的指定索引处。也可以插入单个字符、整数、浮点数
  8. StringBuilder sb = new StringBuilder("Hello, world!");
    sb.insert(7,"hhh");
    //输出Hello, hhhworld

  9. replace(int start, int end, String str) 用指定的字符串替换此字符串构建器中从start到end的子字符串。
  10. StringBuilder sb = new StringBuilder("Hello World");  
    sb.replace(6, 11, "Planet");
     // replaces "World" with "Planet" in the string, resulting in "Hello Planet"

  11. reverse() 反转此字符串构建器中的字符顺序。
  12. StringBuilder sb = new StringBuilder("Hello World");  
    sb.reverse(); // reverses the string to "dlroW olleH"

  13. toString() 返回此字符串构建器中的字符串。
  14. String str = sb.toString();  
    System.out.println(str);  // 输出: Hello, world!  

  15. length() 返回此字符串构建器中的字符数。
  16. StringBuilder sb = new StringBuilder("Hello, world!");
    int len = sb.length();   //输出12

  17. capacity() 返回此字符串构建器的容量
  18. StringBuilder sb = new StringBuilder("Hello, world!");
    System.out.println(sb.capacity());  // 输出: 16  
    //这是因为在默认情况下,StringBuilder 实例的容量是 16。如果需要更多空间,可以调用 
    ensureCapacity() 方法预先分配足够的空间。

  19. ensureCapacity(int minCapacity) 如果需要,增加此字符串构建器的容量,以便它至少可以容纳由参数指定的最小容量。
  20. StringBuilder sb = new StringBuilder("Hello, world!");
    sb.ensureCapacity(20);  // 增加容量至少为20  

  21. charAt(int index) 返回此字符串构建器中指定索引处的字符。
  22. StringBuilder sb = new StringBuilder("Hello, world!");
    char c = sb.charAt(5); // c 会得到字符 'o'

  23. substring(int start, int end) 返回此字符串构建器中从start到end的子字符串。
  24. StringBuilder sb = new StringBuilder("Hello, world!");
    String sub = sb.substring(2, 7); // sub 会得到子字符串 "llo, "

  25. indexOf(String str) 返回指定子字符串在此字符串构建器中第一次出现的索引。
  26. StringBuilder sb = new StringBuilder("Hello, world!");
    int index = sb.indexOf("world"); // index 会得到7,因为 "world" 从索引7开始

  27. lastIndexOf(String str) 返回指定子字符串在此字符串构建器中最后一次出现的索引。
  28. StringBuilder sb = new StringBuilder("Hello, world world!");
    int lastIndex = sb.lastIndexOf("world"); // lastIndex 会得到13

  29. indexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串在此字符串构建器中第一次出现的索引。
  30. StringBuilder sb = new StringBuilder("Hello, world!");
    // 查找子字符串 "world" 从索引位置 7 开始的第一次出现  
    int index = sb.indexOf("world", 7);    
    System.out.println(index);  // 输出: 7

  31. lastIndexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串在此字符串构建器中最后一次出现的索引。
  32. StringBuilder sb = new StringBuilder("Hello, world!");
    int lastindex = sb.lastIndexOf("world", 7);    
    System.out.println(lastindex);  // 输出: 7

  33. equals(Object anObject) 将此字符串构建器与指定的对象比较。
  34. StringBuilder sb1 = new StringBuilder("Hello, World!");  
    StringBuilder sb2 = new StringBuilder("Hello, World!");  
    StringBuilder sb3 = new StringBuilder("Hello, Java!");  
    System.out.println(sb1.equals(sb2));  // 输出: true  
    System.out.println(sb1.equals(sb3));  // 输出: false  

  35. equalsIgnoreCase(String anotherString) 按字典顺序比较此字符串构建器和另一个字符串,不区分大小写,不等时为false,否则为true。
  36. StringBuilder sb1 = new StringBuilder("Hello, World!");  
    StringBuilder sb2 = new StringBuilder("hello, world!");  
    System.out.println(sb1.equalsIgnoreCase(sb2));  // 输出: true  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值