Java中StringBuffer类常用方法介绍

  • StringBuffer类的介绍

StringBuffer是字符串缓存区,当new的时候是在堆内存创建了一个对象,底层是一个长度为16的
字符数组当调用添加的方法时,不会再重新创建对象,在不断向原缓冲区添加字符

  • 查看字符串缓存区容量和长度(字符数)——capacity/length

StringBuffer x1 = new StringBuffer();
System.out.println(x1.length());     //容器中的字符个数,实际值
System.out.println(x1.capacity());   //容器的初始容量,理论值
  • 字符串缓存区——添加(append)

#可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身

#StringBuffer类中重写了toString方法,显示的是对象中的属性值

StringBuffer x = new StringBuffer();
x.append("hello ");  
x.append("the ");
x.append("world");
System.out.println(x);
  • 字符串缓存区——插入(insert)

StringBuffer x = new StringBuffer("happy");
x.insert(2,"ss");  //在指定位置添加元素
System.out.println(x);
  • 字符串缓存区——替换(replace)

StringBuffer x = new StringBuffer("happy");
x.replace(3,5,"cc");//替换指定位置字符串(左闭右开)
System.out.println(x);
  • 字符串缓存区——反转(reverse)

StringBuffer x = new StringBuffer("happy");
System.out.println(x.reverse()); //反转字符串
  • 字符串缓存区——截取(substring)

StringBuffer x = new StringBuffer("happy");
System.out.println(x.substring(2,6));//截取字符串
  • 字符串缓存区——删除

删除从指定位置开始指定位置结束的内容,并返回删除后的字符缓冲区本身——delete

StringBuffer sb = new StringBuffer("hello");
System.out.println(sb.delete(1,2));

根据索引删除掉索引位置上对应的字符,返回删除后的字符缓冲区本身—deleteCharAt

StringBuffer sb = new StringBuffer("hello");
System.out.println(sb.deleteCharAt(4));
  • 字符串缓存区——清空

方法一:(推荐)

StringBuffer x = new StringBuffer("hello");
System.out.println(x.length());
System.out.println(x.capacity());
x.delete(0,x.length());  //清空缓存区
System.out.println(x.length());
System.out.println(x.capacity());

方法二:

//方式二:不建议这种方式清空缓存区,原来的会变成垃圾,浪费内存
StringBuffer x1 = new StringBuffer("hello");
x1 = new StringBuffer();
System.out.println(x1.length());
System.out.println(x1.capacity());
  • StringBuffer和String相互转换

StringBuffer转换成String

通过构造方法
StringBuffer x1 = new StringBuffer("hello");
String x2 = new String(x1);
System.out.println(x2);
通过toString方法
StringBuffer x1 = new StringBuffer("hello");
String x3 = x1.toString();
System.out.println(x3);
通过截取字符串方法
StringBuffer x1 = new StringBuffer("hello");
String x4 = x1.substring(0,x1.length());
System.out.println(x4);

String转化成StringBuffer

通过构造方法
StringBuffer s1 = new StringBuffer("help");
System.out.println(s1);
通过append方法
StringBuffer s2 = new StringBuffer();
s2.append("help");
System.out.println(s2);
  • 当做参数传递时,String和StringBuffer区别

#当做参数传递时原则:基本数据类型的值传递,不改变其值;引用数据类型的值传递,改变其值

#String类虽然是引用数据类型,但是他当作参数传递时和基本数据类型是一样的,不改变其值

#StringBuffer作为参数传递,改变其值

  • 1
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: JavaStringBuffer是一个可变的字符串,它提供了许多常用方法,包括: 1. append():将指定的字符串追加到当前字符串的末尾。 2. insert():将指定的字符串插入到当前字符串的指定位置。 3. delete():删除当前字符串指定位置的字符。 4. deleteCharAt():删除当前字符串指定位置的字符。 5. replace():用指定的字符串替换当前字符串指定位置的字符。 6. substring():返回当前字符串指定位置的子字符串。 7. length():返回当前字符串的长度。 8. capacity():返回当前字符串的容量。 9. setLength():设置当前字符串的长度。 10. trimToSize():将当前字符串的容量调整为当前字符串的长度。 以上是Java StringBuffer常用方法,可以根据需要选择使用。 ### 回答2: JavaStringBuffer是一个常用的字符串操作,它提供了对字符串进行增删改查等操作的方法,下面就是常用的一些方法: 1. append(String str):将指定的字符串追加到此字符序列。 2. insert(int offset, String str):将指定的字符串插入此字符序列的指定位置。 3. delete(int start, int end):从此字符序列删除指定范围的字符。 4. deleteCharAt(int index):从此字符序列删除指定位置的字符。 5. replace(int start, int end, String str):用指定的字符串替换此字符序列指定范围内的字符。 6. substring(int start):返回一个新的字符串,它是此字符串从指定位置开始到结尾的一个子字符串。 7. substring(int start, int end):返回一个新的字符串,它是此字符串从指定位置开始到指定位置结束的一个子字符串。 8. length():返回此字符序列的长度。 9. charAt(int index):返回指定位置的字符。 10. setCharAt(int index, char ch):将指定位置的字符设置为指定的字符。 11. toString():返回此字符序列的字符串表示形式。 总的来说,StringBuffer提供了丰富的字符串操作方法,可以方便地进行字符串的拼接、删除、替换等操作。因此,在Java编程StringBuffer常用之一。 ### 回答3: JavaStringBuffer是一个可变的字符串,使用StringBuffer可以方便地进行字符串的修改和操作。它提供了许多常用方法,下面介绍一些常用方法及其用法: 1. append():向字符串后面追加字符或字符串。例如: StringBuffer sb = new StringBuffer("hello"); sb.append("world"); System.out.println(sb.toString()); 输出结果为:helloworld 2. delete():删除字符串指定位置的字符。例如: StringBuffer sb = new StringBuffer("hello"); sb.delete(1, 3); // 删除从1到3的字符,不包括3 System.out.println(sb.toString()); 输出结果为:hlo 3. insert():在字符串指定位置插入字符或字符串。例如: StringBuffer sb = new StringBuffer("hello"); sb.insert(3, "world"); // 在3的位置插入world System.out.println(sb.toString()); 输出结果为:helworldlo 4. reverse():将字符串反转。例如: StringBuffer sb = new StringBuffer("hello"); sb.reverse(); System.out.println(sb.toString()); 输出结果为:olleh 5. setCharAt():将字符串指定位置的字符替换为指定的字符。例如: StringBuffer sb = new StringBuffer("hello"); sb.setCharAt(1, 'a'); // 将第一个字符替换为a System.out.println(sb.toString()); 输出结果为:hallo 6. toString():将StringBuffer对象转换为字符串。例如: StringBuffer sb = new StringBuffer("hello"); String str = sb.toString(); System.out.println(str); 输出结果为:hello 7. length():返回字符串的长度。例如: StringBuffer sb = new StringBuffer("hello"); int len = sb.length(); System.out.println(len); 输出结果为:5 8. capacity():返回字符串的容量。例如: StringBuffer sb = new StringBuffer("hello"); int cap = sb.capacity(); System.out.println(cap); 输出结果为:21 除了上述常用方法之外,还有一些其他比较有用的方法,例如indexOf()、substring()、replace()等,都可以参考Java API文档进行了解。总的来说,JavaStringBuffer提供了多种灵活的方法,让我们可以方便地对字符串进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值