其他分享
首页 > 其他分享> > stringBuffer详解

stringBuffer详解

作者:互联网

stringBuffer与string

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

工作原理

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

StringBuffer与String的区别:

StringBuffer 的构造方法:

StringBuffer sb=new StringBuffer();
StringBuffer sb1 = new StringBuffer(50);
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
// 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类的主要区别

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

小结

End

标签:sb1,stringBuffer,System,详解,StringBuffer,println,sb,out
来源: https://blog.csdn.net/qq_45872399/article/details/121255387