stringBuffer详解

stringBuffer与string

stringBuffer简介 :StringBuffer是一个字符的缓冲区,如果需要频繁的对字符串进行拼接时,建议使用StringBuffe。

工作原理

StringBuffer的底层是一个char类型的数组,如果没有明确设定,则系统会自动创建一个长度为16的char类型数组,在使用数组的时候,如果长度不够了,则会通过拷贝对数组进行扩容,所以使用StringBuffe时最好预测并且手动进行初始化长度,这样能够减少数组的拷贝,从而提高效率。

StringBuffer与String的区别:

  • string是不可变字符序列,储存在字符串常量池中
  • stringBuffer的底层是char类型数组,系统会对该数组进行扩充
  • StringBuffer: 线程安全的可变字符串
  • 我们如果对字符串进行拼接操作,每次拼接都会构成一个新的String对象,既耗时,又浪费空间
  • 前者长度和内容可变,后者不可变

StringBuffer 的构造方法:

  • 如果没有明确指出参数长度的话,系统会自动给一个16的长度参数。,如下创建:
StringBuffer sb=new StringBuffer();
  • 如果明确指定容量的字符串缓冲区对象,则长度为指定的大小。如下创建:
StringBuffer sb1 = new StringBuffer(50);
  • 指定字符串内容的字符串作为缓冲区对象,则长度大小为:StringBuffer的长度就是这个String类型的字符串长度+16
StringBuffer sb2 = new StringBuffer("hello"); // StringBuffer的长度为21
  • 给出具体代码测试:
StringBuffer sb = new StringBuffer();
System.out.println("sb-->" + sb);
System.out.println("sb-->" + sb.capacity());
System.out.println("sb--" + sb.length());
System.out.println("=============================");
StringBuffer sb1 = new StringBuffer(50);
System.out.println(sb1);
System.out.println(sb1.capacity());
System.out.println(sb1.length());
System.out.println("=======================");
StringBuffer sb2 = new StringBuffer("hello");
System.out.println(sb2);
System.out.println(sb2.capacity());
System.out.println(sb2.length());


sb-->							// 没有指定对象,则创建的对象为空
sb-->16						// 容量
sb--0							// 实际的stringbuffer的长度length
=============================
								// 没有指定对象,则创建的对象为空
50							// 容量
0								// 实际的stringbuffer的长度length
=======================
hello
21
5
  • StringBuffer与String的相互转换:
// string ----> stringbuffer
		String s = "123";
		StringBuffer sb  = new StringBuffer(s);
		System.out.println("StringBuffer "+sb);
// stringbufffer ---- string 
		StringBuffer sb1 = new StringBuffer("hello");
		String ss = sb1.toString();
		System.out.println("string "+ss);

需要注意的是,StringBuffer和String属于不同的类型(stringbuffer是一个单独的类型),也不能直接进行强制类型转换,下面的代码都是错误的:

StringBuffer s = “abc”; //赋值类型不匹配
StringBuffer s = (StringBuffer)”abc”; //不存在继承关系,无法进行强转

stringBuffer的常用方法

​ StringBuffer类中的方法主要偏重于对于字符串的变化,例如追加、插入和删除等,这个也是StringBuffer和String类的主要区别

  • Capacity() 返回当前容量 理论值

  • length() 返回长度(字符数) 实际值

  • append方法

    • 该方法的作用是追加内容到当前StringBuffer对象的末尾,类似于字符串的连接。该方法以后,StringBuffer对象的内容也发生改变。
    • 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
  • insert方法

    • 该方法的作用是在StringBuffer对象中插入内容,然后形成新的字符串。
    • insert(int offset,String str),在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
  • deleteCharAt(int index)方法

    • 删除指定位置字符,并返回本身stringbuffer对象
  • delete(int start,int end)方法

    • 删除从指定位置开始,并从指定位置结束字符,并返回本身
    • 注意:包左不包右
  • substring(int start)方法

    • StringBuffer的截取功能:注意返回值类型不是StringBuffer,返回值为string类型
    • substring(int start,int end)
  • replace (int start,int end,String str) 方法

    • StringBuffer的替换功能:从start到end用 str 替换
  • reverse()方法

    • StringBuffer的反转功能:注意返回值类型仍是StringBuffer,可以通过toString()方法转换为string类型
  • codePointBefore(int index)方法

    • 返回字符串中指定索引之前的字符的Unicode值。第一个字符的索引为1,第二个字符的索引为2
    • index 值0将产生错误,因为这是一个负数(超出范围) index从1开始
  • setCharAt()方法

    • 该方法的作用是修改对象中索引值为index位置的字符为新的字符ch
  • trimToSize()方法

    • 该方法的作用是将StringBuffer对象的中存储空间缩小到和字符串长度一样的长度,减少空间的浪费。
  • …等等方法 ,大家可以自行测试

  • 测试代码

StringBuffer sb1 = new StringBuffer();
sb1.append("hello");
sb1.append(1);
sb1.append(1.2);
sb1.append(true);
System.out.println("---->>>>>>>");
System.out.println(sb1);
System.out.println("sb1 capacity  " + sb1.capacity());
System.out.println("sb1 len  " + sb1.length());
System.out.println("------------------");
sb1.append("hello").append(1).append(1.2).append(true);
System.out.println(sb1);
System.out.println("sb1 capacity  " + sb1.capacity());
System.out.println("sb1 len  " + sb1.length());
System.out.println("----------");
sb1.insert(5, 2222);
sb1.insert(0, 11111);
System.out.println(sb1);
System.out.println("sb1 capacity  " + sb1.capacity());
System.out.println("sb1 len  " + sb1.length());
	

---->>>>>>>
hello11.2true
sb1 capacity  16
sb1 len  13
------------------
hello11.2truehello11.2true
sb1 capacity  34
sb1 len  26
----------
11111hello222211.2truehello11.2true
sb1 capacity  70
sb1 len  35
StringBuffer sb = new StringBuffer();
sb.append("hello").append("world").append("is").append("big");
System.out.println(sb);
sb.delete(0, 5);
System.out.println(sb);
sb.deleteCharAt(0);
System.out.println(sb);
sb.delete(0, sb.length());
System.out.println("sb  " + sb);


helloworldisbig
worldisbig
orldisbig
sb 
StringBuffer sb = new StringBuffer();
sb.append("hello").append("wolrd").append("java");
System.out.println(sb);
String s = sb.substring(5);
System.out.println(s);
String ss = sb.substring(5, 10);
System.out.println(ss);
System.out.println("====================");
StringBuffer sb1 = new StringBuffer();
sb1.append("this is good"); // 空格也算一个大小
System.out.println(sb1);
System.out.println(sb1.capacity());
System.out.println(sb1.length());


hellowolrdjava
wolrdjava
wolrd
====================
this is good
16
12
StringBuffer sb  = new StringBuffer();
sb.append("this is good");
sb.replace(0, 4, "after");
System.out.println(sb);


after is good
StringBuffer sb  = new StringBuffer();
sb.append("this is a good");
//		sb.reverse();   // 结果还是一个stringbuffer类型,结果在内存里
String ss  = sb.reverse().toString(); // 将反转的结果,转变为一个string类型输出
//		System.out.println(sb);
System.out.println(ss);


doog a si siht
StringBuffer sb  = new StringBuffer();
sb.append("this is a good");
System.out.println(sb);
System.out.println(sb.capacity());
System.out.println(sb.length());
sb.append(" yes");
System.out.println(sb);
System.out.println(sb.capacity());
System.out.println(sb.length());
StringBuffer sb1 = new StringBuffer(50);
System.out.println(sb1);
System.out.println(sb1.capacity());
System.out.println(sb1.length());
StringBuffer sb2 = new StringBuffer("hello");
System.out.println(sb2);
System.out.println(sb2.capacity());
System.out.println(sb2.length());


this is a good
16
14
this is a good yes
34
18

50
0
hello
21
5
// string ----> stringbuffer
String s = "123";
StringBuffer sb  = new StringBuffer(s);
System.out.println("StringBuffer "+sb);
// stringbufffer ---- string 
StringBuffer sb1 = new StringBuffer("hello");
String ss = sb1.toString();
System.out.println("string "+ss);


StringBuffer 123
string hello
Scanner scanner = new Scanner(System.in);
String s  = scanner.next();
System.out.println(s);
// 预判断 用户是否还有下一个输入
if (scanner.hasNext()) {
  // 如果有下一个输入,将这个结果赋值给ss
  String ss = scanner.next();
  System.out.println(ss);
}
StringBuffer sb  = new StringBuffer();
sb.append("this is good");
System.out.println(sb);

sb.insert(0, 1111);
System.out.println(sb);

sb.replace(0, 4, "2222");
System.out.println(sb);
sb.replace(0, 4, "333");
System.out.println(sb);

sb.delete(0, 4);
System.out.println(sb);

sb.deleteCharAt(4);
System.out.println(sb);

String ss = sb.substring(4);
System.out.println(sb); // 截取对原的stringbuffer,没影响,相当于拷贝,截取
System.out.println(ss);

StringBuffer sss = sb.reverse();
String ssss = sb.reverse().toString();

sb.setCharAt(0, 't');
System.out.println(sb);
System.out.println(sb.capacity());
System.out.println(sb.length());

sb.trimToSize();
System.out.println(sb);
System.out.println(sb.capacity());
System.out.println(sb.length());
// codePointBefore()方法返回字符串中指定索引之前的字符的Unicode值。第一个字符的索引为1,第二个字符的索引为2
// 值0将产生错误,因为这是一个负数(超出范围) index从1开始
int code = sb.codePointBefore(1);
System.out.println(code);
System.out.println(sb);

// x 不能直接.toString() ; 要把一个整数变为字符串 ,需要包装类integer.toString(x)
// 其他基本类型变为字符串也是用到包装类
int x= 10;
Integer.toString(x);


this is good
1111this is good
2222this is good
333this is good
his is good
his s good
his s good
s good
tis s good
16
10
tis s good
10
10
116
tis s good

小结

  • 相信看到这个的小伙伴,已经弄明白了stringBuffer的使用方法

    ​ 学而不敲则罔,思而不敲则殆

stringBuffer详解

上一篇:转拼音


下一篇:leetcode刷题(字符串)