Java源码___AbstractStringBuilder类(五)

java.lang.AbstractStringBuilder分析摘要:
<1>subSequence(int start, int end)方法
<2>indexOf(String str, int fromIndex)方法
<3>lastIndexOf重载方法
<4>reverse()方法
<5>getValue()方法
<6>toString()方法

1.subSequence(int start, int end)方法
 该方法和substring的用法一样,不过这个返回的是字符序列类。

public CharSequence subSequence(int start, int end){
    return substring(start, end);
}

这个方法是的属性有:public公有。
参数:start
参数说明:int类型,开始截取的下标位置。
参数:end
参数说明:int类型,结束截取的下标位置。
返回值:CharSequence类型
返回值说明:返回截取的字符序列。

该方法的总结:其本质就是调用substring(int start, int end)方法。

 
2. indexOf(String str, String formIndex)方法
 获取指定字符串的在字符缓存区中出现的下标位置。

 public int indexOf(String str, int fromIndex){
     return String.indexOf(value, 0, count, str, fromIndex);
 }

这个方法是的属性有:public公有。
参数:str
参数说明:字符串,要查找的字符串
参数:fromIndex
参数说明:int类型,从某个位置开始的寻找。
返回值:int
返回值说明:返回查找到的位置,没找到返回-1。


 
3.lastIndexOf重载方法
 该方法与indexOf相对应,虽然也是获取下标位置,不过是从后面开始往前比较的。

public int lastIndexOf(String str){
    return lastIndexOf(str, count);
}

public int lastIndexOf(String str, int fromIndex){
    return String.valueOf(value, 0, count, str, fromIndex);
}

这个方法是的属性有:public公有。
参数:str
参数说明:字符串类型,要寻找到的字符串
参数:fromIndex
参数说明:int类型,从哪个下标位置开始寻找。
返回值:int
返回值说明:返回找到的下标位置。


4. reverse()方法

 该方法将this对象的字符缓存区字符反转,然后返回this对象。

public AbstractStringBuilder reverse(){
    boolean hasSurrogates = false;
    int n = count - 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;
        vlaue[k] = cj;
        if(Character.isSurrogates(cj) || Character.isSurrogates(ck)){
            hasSurrogates = true;
        }
    }
    if(hasSurrogates){
        //该方法为私有,代码在下面
        reverseAllValidSurrogatePairs();
    }
    return this;
}

private void reverseAllValidSurrogatePairs(){
    for(int i = 0; i<count-1; i++){
        char c2 = value[i];
        if(Character.isLowSurrogate(c2)){
            char c1 = value[i+1];
            if(Character.isHighSurrogate(c2)){
                value[i++] = c1;
                value[i] = c2;
            }
        }
    }
}

这个方法是的属性有:public公有。
返回值:AbstractStringBuilder类
返回值说明:将this对象的字符缓存区字符反转,然后返回this对象。


5. getValue()方法

 该方法是获取字符缓存区的字符。

final char[] getValue(){
    return value;
}

这个方法是的属性有:public公有、final不可改变。
返回值:char[]数组
返回值说明:将this对象的字符缓存区字符反转,然后返回this对象。

6. toString()方法

 该方法为实现,为抽象方法。

public abstract String toString();

猜你喜欢

转载自blog.csdn.net/pseudonym_/article/details/80607077