当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
StringBuffer 和 StringBuilder 类
在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,所以如果需要对字符串进行修改推荐使用 StringBuffer。
StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。
创建 StringBuffer 类
以下是 StringBuffer 类支持的主要方法:
序号 | 方法 | 描述 |
---|---|---|
1 | StringBuffer append(String s) | 将指定的字符串追加到此字符序列。 |
2 | StringBuffer reverse() | 将此字符序列用其反转形式取代。 |
3 | delete(int start, int end) | 移除此序列的子字符串中的字符。 |
4 | insert(int offset, int i) | 将int 参数的字符串表示形式插入此序列中。 |
5 | insert(int offset, String str) | 将str 参数的字符串插入此序列中。 |
6 | replace(int start, int end, String str) | 使用给定String 中的字符替换此序列的子字符串中的字符。 |
StringBuffer 类提供了 3 个构造方法来创建一个字符串,如下所示:
- StringBuffer() 构造一个空的字符串缓冲区,并且初始化为 16 个字符的容量。
- StringBuffer(int length) 创建一个空的字符串缓冲区,并且初始化为指定长度 length 的容量。
- StringBuffer(String str) 创建一个字符串缓冲区,并将其内容初始化为指定的字符串内容 str,字符串缓冲区的初始容量为 16 加上字符串 str 的长度。
// 定义一个空的字符串缓冲区,含有16个字符的容量 StringBuffer str1 = new StringBuffer(); // 定义一个含有10个字符容量的字符串缓冲区 StringBuffer str2 = new StringBuffer(10); // 定义一个含有(16+4)的字符串缓冲区,"罗刹海市"为4个字符 StringBuffer str3 = new StringBuffer("罗刹海市"); /* *输出字符串的容量大小 *capacity()方法返回字符串的容量大小 */ System.out.println(str1.capacity()); // 输出 16 System.out.println(str2.capacity()); // 输出 10 System.out.println(str3.capacity()); // 输出 20
追加字符串
StringBuffer 类的 append() 方法用于向原有 StringBuffer 对象中追加字符串。该方法的作用是追加内容到当前 StringBuffer 对象的末尾,类似于字符串的连接。调用该方法以后,StringBuffer 对象的内容也发生了改变
StringBuffer sb = new StringBuffer("Hello "); // 创建一个 StringBuffer 对象 String str = "World!"; sb.append(str); // 向 StringBuffer 对象追加 str 字符串 System.out.println(sb.toString()); // 输出:Hello World!
插入字符
方法insert(int offset, int i),将int i
参数的字符串表示形式插入offset序列中。
public static void main(String[] args) { StringBuffer sb = new StringBuffer("hello jack"); sb.insert(2,5); System.out.println(sb); // 输出:he5llo jack }
delete() 方法
delete(int start, int end) 方法用于移除序列中子字符串的字符。其中,start 表示要删除字符的起始索引值(包括索引值所对应的字符),end 表示要删除字符串的结束索引值(不包括索引值所对应的字符)。该方法的作用是删除指定区域以内的所有字符。索引值的计算同String对象的计算方法。
StringBuffer sb = new StringBuffer("hello jack"); sb.delete(2,5); System.out.println(sb); // 输出:he jack
反转字符串
StringBuffer 类中的 sb.reverse() 方法用于将字符串序列用其反转的形式取代。
StringBuffer sb = new StringBuffer("1234"); sb.reverse(); System.out.println(sb); // 输出:4321
以下列表列出了 StringBuffer 类的其他常用方法:
序号 | 方法 | 描述 |
---|---|---|
1 | int capacity() | 返回当前容量。 |
2 | char charAt(int index) | 返回此序列中指定索引处的char 值。 |
3 | void ensureCapacity(int minimumCapacity) | 确保容量至少等于指定的最小值。 |
4 | void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将字符从此序列复制到目标字符数组dst 。 |
5 | int indexOf(String str) | 返回第一次出现的指定子字符串在该字符串中的索引。 |
6 | int indexOf(String str, int fromIndex) | 从指定的索引处开始,返回第一次出现的指定子字符串在该字符串中的索引。 |
7 | int lastIndexOf(String str) | 返回最右边出现的指定子字符串在此字符串中的索引。 |
8 | int lastIndexOf(String str, int fromIndex) | 返回 String 对象中子字符串最后出现的位置。 |
9 | int length() | 返回长度(字符数)。 |
10 | void setCharAt(int index, char ch) | 将给定索引处的字符设置为ch 。 |
11 | void setLength(int newLength) | 设置字符序列的长度。 |
12 | CharSequence subSequence(int start, int end) | 返回一个新的字符序列,该字符序列是此序列的子序列。 |
13 | String substring(int start) | 返回一个新的String ,它包含此字符序列当前所包含的字符子序列。 |
14 | String substring(int start, int end) | 返回一个新的String ,它包含此序列当前所包含的字符子序列。 |
15 | String toString() | 返回此序列中数据的字符串表示形式。 |