java.lang.AbstractStringBuilder分析摘要:
<1>offsetByCodePoints(int index, int codePointOffset)
<2>getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
<3>setCharAt(int index, char ch)方法
<4>append重载方法
1.offsetByCodePoints(int index, int codePointOffset)
该方法是用于获取某个Unicode解码值在字符缓存区的下标位置。
public int offsetByCodePontes(int index, int codePointOffset){
if(index<0 || index >count){
throw new IndexOutOfBoundsException();
}
return Character.offsetByCodePointsImpl(value, 0,
count, index, codePointOffset);
}
这个方法是的属性有:public公有的。
参数:index
参数说明:从某个位置开始查找
参数:codePointOffset
参数说明:Unideco值,用于查找对应的解码值。
返回值:intl类型
返回值说明:Unicode解码值,查找对应的编码在字符中的位置。
该方法的主要作用是:返回指定位置的char字符的前一位字符的Unixode的解码值。
2. getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
该方法的作用是将字符缓存区的字符复制到一个数组中dst中。
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin){
if(srcBegin < 0){
throw new StringIndexOutOfBoundsException(srcBegin);
}
if((srcEnd<0) || (srcEnd>count)){
throw new StringIndexOutOfBoundsException(srcEnd);
}
if(srcBegin > srcEnd){
throw new StirngIndexOutOfBoundsException("srcBegin > srcEnd");
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd-srcBegin);
}
这个方法是的属性有:public公有。
参数:srcBegin
参数说明:要复制的开始位置(基于this而言)
参数:srcEnd
参数说明:复制的结束位置(基于this而言)
参数:dst
参数说明:char数组,将this字符复制后存放的数组。
参数:dstBegin
参数说明:dst数组存放this的复制字符,其存在的开始位置。
异常:StringIndexOfBoundsException
异常说明:如果传入的下标位置不合法,就会抛出该异常。
返回值:int
返回值说明:返回给定坐标之间的的字符串的Unicode解码数量。
3.setCharAt(int index, char ch)方法
该方法的作用是设置字符串缓存区的字符,将制定的位置的字符替换新字符。
public void setCharAt(int index, char ch){
if((index<0)||(index>=count)){
throw new StringIndexOutOfBoundsException(index);
}
value[index] = ch;
}
这个方法是的属性有:public公有。
参数:index
参数说明:int类型,替换的下标位置
参数:ch
参数说明:char类型,将制定位置替换成该字符。
异常:StringIndexOfBoundsException
异常说明:如果输入的实参index下标位置不存在,则抛出此异常。
该方法的主要作用是:该方法的作用是设置字符串缓存区的字符,将制定的位置的字符替换新字符。
4. append重载方法
该方法的作用是在字符缓存后面添加内容,这也是这个抽象类中最重要的方法之一。
public AbstractStringBuilder append(Object obj){
return append(String.valueOf(obj));
}
public AbstractStringBuilder append(String str){
if(str == null){
return appendNull();
}
int len = str.length();
ensureCapacityInternal(count+length);
str.getChars(0, len, value, count);
count += len;
return this;
}
public AbstractStringBuilder append(StringBuffer sb){
if(sb == null){
return appendNull();
}
int len = sb.length();
ensureCapacityInternal(count+len);
sb.getChars(0, len, value, count);
count += len;
return this;
}
AbstractStringBuilder append(AbstractStringBuilder asb){
if(asb == null){
return appendNull();
}
int len = asb.length();
ensureCapacityInternal(count+len);
asb.getChar(0, len, value, count);
count += len;
return this;
}
public AbstractStringBuilder append(CharSequence s){
if(s == null){
return appendNull();
}
if(s instanceof String){
return this.append((String)s);
}
if(s instanceof AbstractStringBuilder){
return this.append((AbstractStringBuilder)s);
}
return this.append(s, 0, s.length());
}
public AbstractStringBuilder append(CharSequence s, int start, int end){
if(s == null){
s = "null";
}
if((start<0)||(start>end)||(end>s.length())){
throw new IndexOutOfBoundsException("start"+start+",end"
+end+",s.length()"+s.length());
}
int len = end - start;
ensureCapacityInternal(count+len);
for(int i=start,j=count; i<end; i++,j++){
value[j] = s.charAt(i);
}
count += len;
return this;
}
public AbstractStringBuilder append(char[] str){
int len = str.length;
ensureCapacityInternal(count+len);
System.arraycopy(str, 0, value, count, len);
count += len;
return this;
}
public AbstractStringBuilder append(boolean b){
if(b){
ensureCapacityInternal(count+4);
value[count++] = 't';
value[count++] = 'u';
value[count++] = 'r';
value[count++] = 'e';
}else{
ensureCapacityInternal(count+5);
value[count++] = 'f';
value[count++] = 'a';
value[count++] = 'l';
value[count++] = 's';
value[count++] = 'e';
}
return this;
}
public AbstractStringBuilder apeend(char c){
ensureCapacityInternal(count+1);
value[count++] = c;
return this;
}
public AbstractStringBuilder append(int i){
if(i == Integer.MIN_VALUE){
append("-2147483648");
return this;
}
int appendLength = (i<0)?Integer.StringSize(-i)+1
: Integer.StringSize(i);
int spaceNeeded = count + appendLength;
ensureCapacityInternal(spaceNeeded);
Integer.gerChars(i,spaceNeed,value);
count = spaceNeeded;
return this;
}
public AbstractStringBuilder append(long l){
if(l == Long.MIN_VALUE){
append("-9223372036854775808");
return this;
}
int appendedLength = (l<0)?Long.stringSize(-l)+1
: Long.stringSize(l);
int spaceNeeded = count + appendLength;
ensureCapacityInternal(spaceNeeded);
Long.getChars(l,spaceNeeded,value);
count = spaceNeeded;
return this;
}
public AbstractStringBuilder append(float f){
FloatingDecimal.appendTo(f,this);
return this;
}
public AbstractStringBuilder append(double d){
FloatingDecimal.append(d,this);
return this;
}
//appendNull方法,因为是私有的,所以不外加说明
//其功能就是在this对象后加"null"字符
private AbstractStringBuilder appendNull(){
int c = count;
ensureCapacityInternal(c+4);
value[c++] = 'n';
value[c++] = 'u';
value[c++] = 'l';
value[c++] = 'l';
count = c ;
return this;
}
该方法的注意事项:
<1>如果传入的实参为null,则会变成往字符后面添加为”null”的字符串。
<2该方法实现了八种基本类型的添加方法。代码基本上比较简洁易懂,这边不做过于解释。