Java - 常用类 - StringBuffer类

StringBuffer类基本介绍

  • java.lang.StringBuffer代表可变的字符序列,可以对字符串内容进行增删
  • 很多方法与String相同,但StringBuffer是可变长度的
  • StringBuffer是一个容器

String VS StringBuffer

  1. String保存的是字符串常量,里面的值不能更改,每次String类的更新实际上就是更改地址,效率较低 //private final char value[];
  2. StringBuffer保存的是字符串变量,里面的值可以更改,每次StringBuffer的更新实际上可以更新内容,不用更新地址,效率较高

char[] value;//这个放在堆空间

package com.tao.stringbuffer_;

/**
 * Create By 刘鸿涛
 * 2022/1/2 17:21
 */
public class StringBuffer01 {
    public static void main(String[] args) {
        //1.StringBuffer 的直接父类是 AbstractStringBuilder
        //2.StringBuffer 实现了 Serializable接口,即StringBuffer的对象可以串行化
        //3.在父类中 AbstractStringBuilder 有属性 char[] value,不是final
        //  该 value数组存放 字符串内容,因此存放在堆中
        //4.StringBuffer是一个final类
        //5.因为StringBuffer 字符内容是在 char[]value ,所以在变化(增加/删除)
        //      不用每次都更换地址(即不是每次创建新对象),所以效率高于String
        StringBuffer stringBuffer = new StringBuffer("hello");

    }
}

StringBuffer的构造器

package com.tao.stringbuffer_;

/**
 * Create By 刘鸿涛
 * 2022/1/2 17:34
 */
public class StringBuffer02 {
    public static void main(String[] args) {
        //构造器的使用
        //1.创建一个 大小为 16的 char[],用于存放字符内容
        StringBuffer stringBuffer = new StringBuffer();

        //2.通过构造器指定 char[]大小
        StringBuffer stringBuffer1 = new StringBuffer(100);
        System.out.println(stringBuffer1.length());     //0

        //3.通过 给一个String 创建 StringBuffer,char[] 大小就是
        //      stringBuffer1.length() + 16
        StringBuffer hello = new StringBuffer("hello");
        System.out.println(hello.length()); //5
        System.out.println(hello);          //hello



    }
}

StringBuffer <=> String 相互转换

package com.tao.stringbuffer_;

/**
 * Create By 刘鸿涛
 * 2022/1/2 17:44
 */
public class StringBuffer03 {
    public static void main(String[] args) {
        //看 String -> StringBuffer
        String str = "hello tom";
        //方式1 使用构造器
        //注意:返回的才是StringBuffer对象,对str 本身没有影响
        StringBuffer stringBuffer = new StringBuffer(str);
        System.out.println(stringBuffer + "\t" + str);  //hello tom  hello tom

        //方式2 使用的是append方法
        StringBuffer stringBuffer1 = new StringBuffer();
        stringBuffer1 = stringBuffer1.append(str);
        System.out.println(stringBuffer1);          //hello tom

        //看看 StringBuffer -> String
        //方式1
        StringBuffer stringBuffer2 = new StringBuffer("鬼鬼");
        String s = stringBuffer2.toString();
        System.out.println(s);                      //鬼鬼

        //方式2
        String s1 = new String(stringBuffer2);
        System.out.println(s1);                     //鬼鬼
    }
}

StringBuffer类常见对象

  1. 增 append
  2. 删 delete(start,end)
  3. 改 replace(start,end,string)->将start—end 间的内容替换掉,不含end
  4. 查 indexOf->查找子串在字符串第1次出现的索引,如果找不到返回 -1
  5. 插 insert
  6. 获取长度 length
package com.tao.stringbuffer_;

/**
 * Create By 刘鸿涛
 * 2022/1/2 18:10
 */
public class StringBufferMethod {
    public static void main(String[] args) {
//        1. 增 append
        StringBuffer s = new StringBuffer("hello");
        s.append(",");
        System.out.println(s);      //hello,
        s.append("赵敏").append(100).append(true).append(10.0);
        System.out.println(s);      //实际上是s.toString() -> hello,赵敏100true10.0

//        2. 删 delete(start,end)
        /*
        删除索引为 >= start && end 处的字符
        解读:删除 5 ~ 15 的字符   [11,14)

         */
        s.delete(5,15);
        System.out.println(s);      //hello10.0

//        3. 改 replace(start,end,string)->将start---end 间的内容替换掉,不含end
        s.replace(1,5,"周芷若");  //h周芷若10.0
        System.out.println(s);

//        4. 查 indexOf->查找子串在字符串第1次出现的索引,如果找不到返回 -1
        int s2 = s.indexOf("周芷若");
        System.out.println(s2);     //1

//        5. 插 insert
        s.insert(1,"鬼鬼");
        System.out.println(s);      //h鬼鬼周芷若10.0

//        6. 获取长度 length
        System.out.println(s.length());
    }
}

StringBuffer类练习题目分析

String str = null;
StringBuffer sb = new StringBuffer();
sb.append(str);	//需要看源码,底层调用的是AbstractStringBuilder 的 appendNull
System.out.println(sb.length());	//4	所以字符数组null 4个长度

System.out.println(sb);				//null
StringBuffer sb1 = new StringBuffer(str);	//这里看源码,首先是有参构造器,调用super(str.length() + 16),因为str没有长度,所以会抛出空指针异常
System.out.println(sb1);			//wrong
案例
  • 输入商品名称和商品价格,要求打印效果示例,使用前面学习的方法完成;

  • 商品名 商品价格

  • 手机 123,456.78 //比如 3,456,789.10

  • 要求:价格的小数点前面每三位用逗号隔开,再输出

Mycode

package com.tao.stringbuffer_;

import java.util.Scanner;

/**
 * Create By 刘鸿涛
 * 2022/1/2 18:38
 */
public class StringBufferExercise01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        int point = input.indexOf('.');
        System.out.println(point);
        StringBuffer price = new StringBuffer(input);

        if(point > 3)
            price.insert(point - 3,',');
        if(point > 6)
            price.insert(point - 6,',');
        System.out.println(price);
    }
}

Teacher 's code

package com.tao.stringbuffer_;

import java.util.Scanner;

/**
 * Create By 刘鸿涛
 * 2022/1/2 18:38
 */
public class StringBufferExercise01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        int point = input.indexOf('.');
        System.out.println(point);
        StringBuffer price = new StringBuffer(input);

        for(int i = point; i > 3 ; i -= 3){

            price.insert(i - 3,',');
        }
        System.out.println(price);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鬼鬼骑士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值