StringBuilder类
简介
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。StringBuilder 类在 Java 5 中被提出,它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。在本文中将对StringBuilde类进行一个解析。
首先,StringBuilder类在java.lang包下。其父类是AbstractStringBuilder类,该类实现了Appendable, CharSequence接口
public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence{}
StringBuilder类的常用方法
1.append方法
public StringBuilder append(String str) {//传入一个String对象
super.append(str);//调用父类方法
return this;//返回当前对象。
}
方法描述:将指定的字符串追加到此字符序列。
该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:
public AbstractStringBuilder append(String str) {//传入一个String对象str
if (str == null)//判断当前对象是否为空
return appendNull();//为空返回 该类中appendNull()方法生成的对象,该对象中有一个数组value[3]中存储了null四个字符。
int len = str.length();//len变量中存储当前字符串对象的长度
ensureCapacityInternal(count + len);//调用该类中的private void ensureCapacityInternal(int minimumCapacity)方法传入count + len。
//其中count是原字符串对象中的字符串长度,len为将要拼接的字符串的长度。判断原数组长度是否足够存储拼接后的字符串,不够 则扩容
str.getChars(0, len, value, count);//调用String类中的public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)方法,将str数组从0到len复制到value中,偏移量为count(从第count个开始复制)
count += len;//将当前字符串长度计数重置
return this;//返回新字符串对象
}
2.reverse方法
public StringBuilder reverse() {
super.reverse();//调用父类方法
return this;//返回当前对象
}
方法描述:将此字符序列用其反转形式取代。
该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:
public AbstractStringBuilder reverse() {
//是否含代理字符
boolean hasSurrogates = false;
int n = count - 1; //定义一个变量表示长度-1
//j初始化,长度-2再算术右移一位 j = (count-2)/2
//偶数长度,遍历一半次数,对调替换
//奇数长度,遍历一半-1次数,对调替换,中间值不用替换
for (int j = (n-1) >> 1; j >= 0; j--) {
int k = n - j;
char cj = value[j];
char ck = value[k];
value[j] = ck;
value[k] = cj;
if (Character.isSurrogate(cj) ||
Character.isSurrogate(ck)) {
hasSurrogates = true;
}
}
if (hasSurrogates) {
reverseAllValidSurrogatePairs();
}
return this;
}
3.delete方法
public StringBuilder delete(int start, int end) {//传入一个删除起始点和结束点
super.delete(start, end);//调用父类的delete方法
return this;//返回当前对象
}
方法描述: 移除此序列的子字符串中的字符。
该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:
public AbstractStringBuilder delete(int start, int end) {
if (start < 0)//如果起始下标小于零
throw new StringIndexOutOfBoundsException(start);//抛出字符串角标越界异常
if (end > count)//如果结束点大于最大下标
end = count;//直接将结束点设为最大下标
if (start > end)//如果其实大于结束
throw new StringIndexOutOfBoundsException();//抛出字符串角标越界异常
int len = end - start;//定义变量存储起始下标和结束下标之间的长度
if (len > 0) {//如果删除长度大于零
System.arraycopy(value, start+len, value, start, count-end);//修改字符串数组
count -= len;//重置字符个数
}
return this;//返回当前对象
}
4.insert方法
public StringBuilder insert(int offset, Object obj) {//传入一个偏移量offset和一个字符串对象
super.insert(offset, obj);//调用父类的insert方法
return this;//返回当前对象
}
方法描述:将 Object
参数的字符串表示形式插入此字符序列中。
该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:
public AbstractStringBuilder insert(int offset, Object obj) {
return insert(offset, String.valueOf(obj));//调用该类中的重载方法
}
public AbstractStringBuilder insert(int offset, String str) {//传入一个偏移量offset和一个字符串对象
if ((offset < 0) || (offset > length()))//如果偏移量小于0或者大于该串最长长度
throw new StringIndexOutOfBoundsException(offset);//丢出角标越界异常
if (str == null)//如果对象为空
str = "null";//对象置为null
int len = str.length();//获取当前字符串对象的长度
ensureCapacityInternal(count + len);//扩充字符串数组长度
System.arraycopy(value, offset, value, offset + len, count - offset);//再原字符串对象数组中挪出位置
str.getChars(value, offset);//插入字符串
count += len;//更新当前字符串长度
return this;//返回当前对象
}
5.replace方法
public StringBuilder replace(int start, int end, String str) {//传入一个起始索引,一个终止索引,一个需要替换的字符串序列
super.replace(start, end, str);
return this;
}
方法描述:使用给定 String
中的字符替换此序列的子字符串中的字符。
该方法是重写父类的方法,调用父类的AbstractStringBuilder类的下面方法:
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)//如果起始索引小于字符串序列的最小索引
throw new StringIndexOutOfBoundsException(start);//丢出角标越界异常
if (start > count)//如果起始索引大于字符串序列的最大索引
throw new StringIndexOutOfBoundsException("start > length()");//丢出角标越界异常
if (start > end)//如果起始索引大于终止索引
throw new StringIndexOutOfBoundsException("start > end");//丢出角标越界异常
if (end > count)如果终止索引大于原字符串最大索引
end = count;//将终止索引转换为原字符串最大索引
int len = str.length();//获取传入字符串的长度
int newCount = count + len - (end - start);//设置修改后的字符串长度
ensureCapacityInternal(newCount);//扩容
System.arraycopy(value, end, value, start + len, count - end);//修改字符串数组
str.getChars(value, start);//插入字符串
count = newCount;//更新字符串长度
return this;//返回当前对象
}
下面是几种StringBuilder常用方法例子
public class StringDemo {
public static void main(String[] args) {
StringBuilder stringbulider = new StringBuilder("Chinese");
stringbulider.append(" is good");
System.out.println(stringbulider);
stringbulider.delete(8, 15);
System.out.println(stringbulider);
stringbulider.replace(0, 7, "Person");
System.out.println(stringbulider);
stringbulider.reverse();
System.out.println(stringbulider);
StringBuilder stringbulider1 = new StringBuilder("wh");
String s = "at";
stringbulider1.insert(2,s);
System.out.println(stringbulider1);
}
}
输出:
Chinese is good
Chinese
Person
nosreP
what
其他方法,请自己查阅,谢谢!