这篇文章只记录了一些常用方法,方法实在是太TM多了,吃了String类的亏。
StringBuffer类也称为
字符串缓冲区
。
0x01 添加新字符串
append(任意数据类型)
- 返回值:StringBuffer
注意: 该方法的参数是可以传任意数据类型的对象,然后会将该参数中的对象内容转换成字符串的表示形式追加到该字符序列中。
代码:
StringBuffer sb = new StringBuffer(); sb.append(false); System.out.println(sb); sb.append(1); System.out.println(sb); sb.append("yq2048.cn"); System.out.println(sb); StringBuffer sb1 = new StringBuffer("-淤青的博客"); sb.append(sb1); System.out.println(sb);
重载append
append(CharSequence s,int start,int end)
- 返回值:StringBuffer
- s:追加的字符串
- start:截取s的索引,从start索引开始截取。
- end:从该索引-1的位置结束
截取
。
这些情况会报错:
1.start是负数(因为最小是0,截取第一个字符)
2.start大于end
3.end大于s.length()
代码:
StringBuffer sb = new StringBuffer("淤青"); sb.append("的博客",0,1); System.out.println(sb); sb.append("的博客",1,3); System.out.println(sb);
执行结果:
淤青的
淤青的博客
0x02 返回当前容量
capacity()
- 返回值:int
该方法返回的是容量,而不是长度。
代码:
StringBuffer sb = new StringBuffer(); int a = sb.capacity(); System.out.println(a); sb.append("1"); int b = sb.capacity(); System.out.println(b);
执行结果:
16
16
0x03 返回该索引的字符
charAt(int index)
返回值:char
index:返回该索引位置的内容
代码:
StringBuffer sb = new StringBuffer("淤青的博客"); char c1 = sb.charAt(0); char c2 = sb.charAt(sb.length-1()); System.out.println(c1+" "+c2);
执行结果:
淤 客
0x04 删除指定范围字符
delete(int start,int end)
- 返回值:StringBuffer
- start:从该索引开始(包括该索引),删除后面的字符。
- end:从该索引的-1截止,停止截取字符。
代码:
StringBuffer sb = new StringBuffer("淤青的博客-yq2048.cn"); sb.delete(5,sb.length()); System.out.println(sb);
执行结果:
淤青的博客
注意:
当start是负数时或者大于end或者start大于length()时会报错。
如果end超出了length(),编译时不会报错,而是将后面的内容全部删掉。
0x05 删除指定字符
deleteCharAt(int index)
- 返回值:StringBuffer
- index:删除这个索引位置的字符
StringBuffer sb = new StringBuffer("淤青的博客--yq2048.cn"); sb.deleteCharAt(5); System.out.println(sb);
执行结果:
淤青的博客-yq2048.cn
0x06 将指定内容复制到指定数组
getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin)
- 返回值:void
- srcBegin:从该索引(包括)开始
- srcEnd:从该索引-1结束
- dst:复制到该数组
- dstBegin:在dst中从第几个索引开始粘贴
代码:
StringBuffer sb = new StringBuffer("淤青的博客-yq2048.cn"); char[] c1 = new char[10]; char[] c2 = new char[10]; sb.getChars(0,5,c1,0); //从索引0开始截取,到索引4停止截取(实际)。在c1数组中的索引0处开始粘贴。 sb.getChars(6,sb.length(),c2,1); //在c2数组中的索引1处开始粘贴。 System.out.println(c1); System.out.println(c2);
执行结果:
淤青的博客
yq2048.cn
0x07 返回字符第一次出现的索引
indexOf(String str)
- 返回值:int
- str:需要查找的字符
代码:
StringBuffer sb = new StringBuffer("淤青的博客"); int i = sb.indexOf("客"); System.out.println(i);
执行结果:
4
从指定索引开始搜索字符第一次出现的位置
indexOf(String str,int formIndex)
- 返回值:int
- formIndex:从该索引往后查询
注意: 如果没有查询到有该内容,那么,将返回-1
代码:
StringBuffer sb = new StringBuffer("淤青的博客"); int i1 = sb.indexOf("淤",0); int i2 = sb.indexOf("淤",1); System.out.println(i1+"__"+i2);
执行结果:
0__-1
0x08 将内容插入指定索引
insert(int offset, char[] str)
- 返回值:StringBuffer
- offset:添加到子字符的位置(不是索引)
- srt:将该数组添加到StringBuffer中
该方法的重载方法太多了,几乎都是修改的传输数据类型,这里举例两个重载方法。
代码:
StringBuffer sb = new StringBuffer("淤青的博客"); char[] c = {'y','q','2','0','4','8'}; sb.insert(sb.length(),c); System.out.println(sb);
执行结果:
淤青的博客yq2048.cn
重载insert
insert(int index,char[] str,int offset,int len)
- 返回值:StringBuffer
- index:添加到子字符的位置(不是索引)
- str:需要添加的数组
- offset:从srt中的第几个索引开始截取
- len:添加的数量
代码:
StringBuffer sb = new StringBuffer("淤青的博客"); char[] c = {'y','q',}; sb.insert(2,c,0,2); System.out.println(sb);
执行结果:
淤青yq的博客
0x09 返回字符串最后出现位置的索引
lastIndexOf(String str)
- 返回值:int
- str:寻找的字符
代码:
StringBuffer sb = new StringBuffer("开始的开始,我们都是孩子"); int i = sb.lastIndexOf("始"); System.out.println(i);
执行结果:
4
从指定索引开始搜索字符最后一次出现的位置
lastIndexOf(String str,int formIndex)
- 返回值:int
- formIndex:从该索引往后查询
代码:
StringBuffer sb = new StringBuffer("开始的开始,我们都是孩子"); int i = sb.lastIndexOf("始",5); System.out.println(i);
执行结果:
-1
0x10 返回长度(字符数)
length()
- 返回值:int
注意: length()返回的是当前字符数,而capacity()返回的是具体容量。
代码:
StringBuffer sb = new StringBuffer("淤青的博客"); System.out.println(sb.length()); System.out.println(sb.capacity());
执行结果:
5
21
StringBuffer的默认容量不是16吗?还没有到达触发扩容机制为什么就输出21?
使用无参构造方法的默认容量是16,这里使用的是有参构造方法,而这个构造方法的里的代码是这么写的:super(str.length()+16)
,也就是你参数字符的长度+16。
0x11 指定字符替换指定范围内的字符
replace(int start,int end,String str)
- 返回值:StringBuffer
- start:从该索引位置开始(包括这个索引),被替换。
- end:从end-1的索引位置,停止截取。
·如果str比范围内的字符长度还多,那么将会自动延伸。
代码:
StringBuffer sb = new StringBuffer("害,又在写代码,烦死了。"); sb.replace(8,11,"高兴死了"); System.out.println(sb);
执行结果:
害,又在写代码,高兴死了。
它不仅可以实现替换功能,还可以实现在指定位置添加的功能。
StringBuffer sb = new StringBuffer("大家好,我是小明,来自地球村。"); sb.replace(8,8,",今年18周岁了"); //把第八个索引(是个,)替换掉,替换成今年18周岁了。 System.out.println(sb);
执行结果:
大家好,我是小明,今年18周岁了,来自地球村。
0x12 将字符反转
reverse()
- 返回值:StringBuffer
代码:
StringBuffer sb = new StringBuffer("天行健,君子以自强不息"); sb.reverse(); System.out.println(sb);
执行结果:
息不强自以子君,健行天
0x13 将指定索引处的字符更换成char
setCharAt(int index,char ch)
- 返回值:void
- index:替换该索引的内容
- ch:替换的内容
代码:
StringBuffer sb = new StringBuffer("yq1048.cn"); sb.setCharAt(2,'2'); System.out.println(sb);
执行结果:
yq2048.cn
0x14 设置字符长度
setLength(int newLength)
- newLength:新的长度
代码:
StringBuffer sb = new StringBuffer(); int c1 = sb.capacity(); //返回容量 int l1 = sb.length(); //返回字符长度 System.out.println("更改前:容量:"+c1+",长度:"+l1); sb.setLength(17); //将sb的字符长度更改为17 int c2 = sb.capacity(); int l2 = sb.length(); System.out.println("更改后:容量:"+c2+",长度:"+l2);
执行结果:
更改前:容量:16,长度:0
更改后:容量:34,长度:17
0x15 将内容返回一个新的String
substring(int start)
- 返回值:String
- start:从该索引开始(从这个索引开始截取,包括),截取到最后
代码:
StringBuffer sb = new StringBuffer("yq2048.cn-淤青的博客"); String s1 = sb.substring(10); System.out.println(s1);
执行结果:
淤青的博客
重载substring
substring(int start,int end)
- 返回值:String
- start:从该索引开始截取(截取时包括该索引)
- end:从end-1索引位置停止截取。
代码:
StringBuffer sb = new StringBuffer("开始的开始,我们都是孩子。"); String s1 = sb.substring(6,sb.length()); System.out.println(s1);
执行结果:
我们都是孩子。
版权声明:《 StringBuffer类-常用方法 》为Jack.甄原创文章,转载请注明出处!
最后编辑:2020-8-3 14:08:46