Java API_StringBuffer类


StringBuffer类

1.概述
StringBuffer是线程安全的可变字符串

2.StringBuffer和String的区别?
StringBuffer前者长度和内容可变,后者不可变。
如果使用前者做字符串的拼接,不会浪费太多的资源。

3.StringBuffer的构造方法
public StringBuffer():无参构造方法
public StringBuffer(int capacity):指定容量的字符串缓冲区对象
public StringBuffer(String str):指定字符串内容的字符串缓冲区对象

4.StringBuffer常用方法
(1).长度方法(或者是容量方法)
public int capacity():返回当前的容量。理论值
public int length(): 返回长度(字符数)。实际值

使用举例:
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
System.out.println("---"+sb);
System.out.println("sb.capacity():"+sb.capacity());
System.out.println("sb.length():"+sb.length());
System.out.println("-----------------------");
StringBuffer sb1=new StringBuffer("helloworld");
System.out.println("---"+sb1);
System.out.println("sb.capacity():"+sb1.capacity());
System.out.println("sb.length():"+sb1.length());
}
//输出结果:
---
sb.capacity():16
sb.length():0
-----------------------
---helloworld
sb.capacity():26(16+10)
sb.length():10

(2).添加方法
public StringBuffer append(String str):可以把任意类型的数据添加到字符串缓冲区里面,并返回缓冲区本身。
public StringBuffer insert(int offset,String str):在指定的位置把任意类型的数据插入字符串缓冲区里面,并返回缓冲区本身。

使用举例
public static void main(String[] args) {
StringBuffer sb=new StringBuffer();
sb.append("你好").append("世界").append("---").append("hello").append("world");
System.out.println(sb);
System.out.println("--------------");
sb.insert(2,"test");
System.out.println(sb);
}
//输出结果
你好世界---helloworld
--------------
你好test世界---helloworld

(3).删除功能
public StringBuffer deleteChartAt(int index):删除指定位置的字符,并返回删除以后的字符串缓冲区。
public StringBuffer delete(int start,int end):从指定位置开始到指定位置结束删除,返回字符串本身。

使用举例
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
sb.deleteCharAt(2);
System.out.println(sb);
System.out.println("--------------");
sb.delete(0, 3);
System.out.println(sb);
}
//输出结果
heloworld
--------------
oworld

(4).替换功能
public StringBuffer replace(int strat,int end,String str):从指定位置start开始到end使用str字符串来替换,包左不包右。

使用举例:
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
String str="123";
sb.replace(1, 3, str);
System.out.println(sb);
}
//输出结果
h123loworld

(5).反转功能
public StringBuffer reverse():把原有的字符串倒着输出

使用举例:
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
System.out.println(sb);
System.out.println("------------------------");
sb.reverse();
System.out.println(sb);
}
//输出结果
helloworld
------------------------
dlrowolleh

(6).StringBuffer的截取功能(不再是返回本身,而是返回截取的内容)
public String substring(int start);从指定位置开始截取,把截取的字符串重新赋值给一个字符串
public String substring(int start,int end);从指定位置开始到最后截取,把截取的字符串重新赋值给一个字符串

使用举例
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("helloworld");
String str=sb.substring(3);
System.out.println(str);
System.out.println("----------------------");
String str1=sb.substring(1,3);
System.out.println(str1);
}

//输出结果
loworld
----------------------
el



5.StringBuffer的相关操作

(1).String和StringBuffer之间的相互转化

为什么我们要讲解类之间的转化
String-->StringBuffer
我们可以使用StringBuffer的功能
但是我们需要的还是String类,所以
StringBuffer-->String

代码:
public static void main(String[] args) {
String str ="helloworld";
//String-->StringBuffer
//通过构造
StringBuffer sb =new StringBuffer(str);
//通过apends()方法
StringBuffer sb1=new StringBuffer();
sb1.append(str);
System.out.println("sb:"+sb);
System.out.println("sb1:"+sb1);
System.out.println("-----------------");
//StringBuffer--->String
StringBuffer buffer =new StringBuffer("java");
//通过String构造
String str1=new String(buffer);
//通过String的toString()方法
String str2=buffer.toString();
System.out.println("str1:"+str1);
System.out.println("str2:"+str2);
}
//输出结果
sb:helloworld
sb1:helloworld
-----------------
str1:java
str2:java


6.相关面试题

(1).String,StringBuffer,StringBuilder的区别
String的内容是不可变的,而StringBuffer,StringBuilder的内容可变
StringBuffer是同步的,数据是安全的,效率低;StringBilder是不同步的,数据不安全,效率高。

(2).StringBuffer和数组的区别?
StringBuffer和数组都可以看成是一个容器,用于转载数据
但是呢,StringBuffer的数据最终是一个字符串数据(数据类型可以是不一样的)。
而数组可以放置多个数据,但是必须是同一种数据类型。

(3).形式参数问题(*)
String作为参数传递
StringBuffer作为参数传递

形式参数:
基本类型:形式参数的改变不会影响实际参数
引用类型:形式参数的改变会直接影响实际参数

猜你喜欢

转载自www.cnblogs.com/nwxayyf/p/9465270.html