如何修改StringBuilder类型的字符串的内容?

来源:2-5 认识 Java 中的 StringBuilder 类

雪中_悍刀行

2016-07-23 19:05

既然说String类具有不可变性,而StringBuilder和StringBuffer具有可变性,怎么个可变法,能否具体举例说明?

写回答 关注

3回答

  • 慕仰6620973
    2016-07-23 20:15:43
    已采纳

       String temp = "哇哈哈";
       System.out.println(temp+"1234");    // 两个空间,分别是temp和temp+"1234"
       StringBuilder sb = new StringBuilder("哇哈哈");
       System.out.println(sb.append("1234"));  // 一个空间

    雪中_悍刀行

    吆西

    2016-07-24 09:51:37

    共 1 条回复 >

  • Z华L
    2018-06-12 00:14:30

    有点理解了,一个相当于在原来的位置旁边接着写,另一个相当于把原来的擦掉重写.

  • dyr
    2016-07-23 20:34:23

    delete(int start, int end)

    Removes the characters in a substring of this sequence.

    replace(int start, int end, String str)

    Replaces the characters in a substring of this sequence with characters in the specified String.


Java入门第三季

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

409765 学习 · 4534 问题

查看课程

相似问题