问答详情
源自:2-5 认识 Java 中的 StringBuilder 类

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

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

提问者:雪中_悍刀行 2016-07-23 19:05

个回答

  • 慕仰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"));  // 一个空间

  • 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.