文章目录
StringBuffer类基本介绍
- java.lang.StringBuffer代表可变的字符序列,可以对字符串内容进行增删
- 很多方法与String相同,但StringBuffer是可变长度的
- StringBuffer是一个容器
String VS StringBuffer
- String保存的是字符串常量,里面的值不能更改,每次String类的更新实际上就是更改地址,效率较低 //private final char value[];
- 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类常见对象
- 增 append
- 删 delete(start,end)
- 改 replace(start,end,string)->将start—end 间的内容替换掉,不含end
- 查 indexOf->查找子串在字符串第1次出现的索引,如果找不到返回 -1
- 插 insert
- 获取长度 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);
}
}