StringBuffer类之详解(一)
六、替换功能
StringBuffer
replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specifiedString
.
从start开始到end用str替换
@Override
public synchronized StringBuffer replace(int start, int end, String str) {
toStringCache = null;
super.replace(start, end, str);
return this;
}
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");
//end大于缓冲区长度,则替换到最后一位
if (end > count)
end = count;
int len = str.length();
//包含头,不包含尾的替换
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
//缓冲区字符数组从end开始将count-end长度复制到该数组start+len处开始
System.arraycopy(value, end, value, start + len, count - end);
//start开始插入str字符串
str.getChars(value, start);
count = newCount;
return this;
}
七、反转功能
StringBuffer
reverse()
Causes this character sequence to be replaced by the reverse of the sequence.
字符串反转
@Override
public synchronized StringBuffer reverse() {
toStringCache = null;
super.reverse();
return this;
}
public AbstractStringBuilder reverse() {
boolean hasSurrogates = false;
//n为缓冲区数组下标
int n = count - 1;
//j相当于(n-1)/2
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;
}
八、截取功能
String
substring(int start)
Returns a newString
that contains a subsequence of characters currently contained in this character sequence.String
substring(int start, int end)
Returns a newString
that contains a subsequence of characters currently contained in this sequence.从指定位置截取到末尾
截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
@Override
public synchronized String substring(int start) {
return substring(start, count);
}
@Override
public synchronized String substring(int start, int end) {
return super.substring(start, end);
}
public String substring(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
throw new StringIndexOutOfBoundsException(end);
if (start > end)
throw new StringIndexOutOfBoundsException(end - start);
return new String(value, start, end - start);
}
注:返回类型不在是StringBuffer对象,而是字符串对象,如需使用,要及时接受返回数据。
九、String转化为StringBuffer
方式一:
通过StringBuffer构造方法
StringBuffer sb = new StringBuffer("123");
方式二:
通过StringBuffer的append()方法
StringBuffer sb2 = new StringBuffer();
sb2.append("123"); //通过append方法将字符串转换为StringBuffer对象
十、StringBuffer转化为String
方式一:
通过String构造方法
StringBuffer sb = new StringBuffer("123");
String s1 = new String(sb);
方式二:
通过StringBuffer的toString()方法
StringBuffer sb = new StringBuffer("123");
String s2 = sb.toString();
方式三:
通过StringBuffer的subString(0,length);
StringBuffer sb = new StringBuffer("123");
String s3 = sb.substring(0, sb.length());
十一、String、StringBuffer作为参数传递
基本数据类型作为参数时,进行的是传值,不改变原内容
引用数据类型作为参数时,进行的是传址,改变原内容
String虽然是引用数据类型,但是作为参数时他的传递是和基本数据类型一样