Java源码___String类(七)

java.lang.String分析摘要:
<1>lastIndexOf重载方法
<2>substring重载方法
<3>subSequence(int beginIndex, int endIndex)
<4>concat(String str)
<5>replace重载方法

1.lastIndexOf重载方法
 该方法与indexOf方法相对应,该方法是用某个位置往前查找第一个出现所要查找字符的位置(下标)。

//从该字符串结尾处往前搜索第一次出现ch的坐标
public int lastIndexOf(int ch){
    return lastIndexOf(ch, value.length-1);
}

//从该字符串某个位置开始往前搜索第一次出现ch的坐标
public int lastIndexOf(int ch, int fromIndex){
    if(ch < Character.MIN_SUPPLEMENTARY_CODE_POINT){
        final char[] value = this.value;
        int i = Math.min(fromIndex, value.length-1);
        for(;i >=0 ; i--){
            if(value[i] == ch){
                return i;
            }
        }
        return -1;
    }else{
        return lastIndexOfSupplementary(ch, fromIndex);
    }
}

//lastIndexOfSupplementary方法是处理辅助字符的操作
private int lastIndexOfSupplementary(int ch, int fromIndex){
    if(Character.isValidCodePoint(ch)){
        final char[] value = this.value;
        char hi = Character.highSurrogate(ch);
        char lo = Character.lowSurrogate(ch);
        int i = Math.min(fromIndex, value.length-1);
        for(;i>=0;i--){
            if(value[i]==hi && value[i+1]==lo){
                return i;
            }
        }
    }
    return -1;
}

static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount, int fromIndex){
    int rightIndex = sourceCount - targetCount;
    if(formIndex <0 ){
        return -1;
    }
    if(fromIndex > rightIndex){
        fromIndex = rightIndex;
    }
    if(targetCount == 0){
        return fromIndex;
    }
    int strLastIndex = targetOffset + targetCount -1;
    char strLastChar = target[strLastIndex];
    int min = sourceOffset+sourceCount-1;
    int i = min + fromIndex;

startSearchForLastChar:
    while(true){
        while(i>=min && source[i]!=strLastChar){
            i--;
        }
        if(i<min){
            return -1;
        }
        int j = i-1;
        int start = j-(targetCount-1);
        int k = strLastIndex - 1;
        while(j > start){
            if(source[j--] != target[k--]){
                i--;
                continue startSearchForLastChar;
            }
        }
        return start-sourceOffset+1;
    }
}

static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
                       String target, int fromIndex){
    return lastIndexOf(source, sourceOffset, sourceCount,
                       target.value, 0, target.value.length, fromIndex);
}

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

这个方法是的属性有:public公有的。
参数:ch要查找的字符
参数:fromIndex从后往前开始搜索的下标位置

返回值:int类型
返回值说明:如果找到则返回该字符的下标,否则返回-1。

2. substring重载方法
 该方法的作用是截取字符串,返回字符串的子串。

public String substring(int beginIndex){
    if(beginIndex <0 ){
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    int subLen = value.length - beginIndex;
    if(subLen < 0){
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return (beginIndex==0) ? this : new String(value, beginIndex, subLen);
}

public String substring(int beginIndex, int endIndex){
    if(beginIndex < 0){
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if(endIndex > value.length){
        throw new StirngIndexOutOfBoundsException(endIndex);
    }
    int subLen = endIndex - beginIndex;
    if(subLen < 0){
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return ((beginIndex==0) && (endIndex == value.length)) ? 
            this : new String(value, beginIndex, subLen);
}

这个方法是的属性有:public公有。
参数:beginIndex开始截取的下标位置
参数:endIndex 截取结束的下标位置
异常:StringIndexOutOfBoundsExcepiton
异常说明:如果开始截取的下标超过字符串的界限,则会抛出该异常。

返回值:String类型
返回值说明:返回截取的字符串。

该源码的思路是:
<1>判断下标是否超过字符串边界,如果超过边界则抛出S他ringIndexOfBoundsException异常。
<2>如果没问题,则用String的构造函数String(char[] value, int offset, int count)来构造一个字符串返回。

 

3.subSequence(int beginIndex, int endIndex)方法
 该方法返回一个字符序列,它是这个序列的一个子序列。

public CharSequence subSequence(int beginIndex, int endIndex){
    return this.subString(beginIndex, endIndex);
}

这个方法是的属性有:public公有。
返回值:CharSequence类型
返回值说明:CharSequence类型和String类型的主要区别是CharSequence是可读可写,而String是只读的。

该方法的源码思路是:
<1>底层本质调用的方法还是subString(int beginIndex, int endIndex)方法。

 

4. concat(String str)方法

 该方法的作用是将实参str追加到this字符串的末尾。

public String concat(String str){
    int otherLen = str.length;
    if(otherLen == 0){
        return this;
    }
    int len = value.length;
    char[] buf = Array.copyOf(value, len+otherLen);
    str.getChars(buf, len);
    return new String(buf,true);
}

这个方法是的属性有:public公有。
参数:str要追加的字符串
返回值:String
返回值说明:在本字符串的末尾拼接str字符串并返回。


该源码思路为:
<1>判断追加的字符串str是否为空字符串,是的话返回this字符串。
<2>创建新字符数组,长度为this和str的长度之和,然后将两者赋值到数组buf。
<3>用方法String(char[] value, boolean share)构建新字符串,将其返回。

5. replace重载方法

 该方法的基本作用是进行替换。

//会将本字符串中的出现的所有字符oldChar替换成newChar字符。
public String replace(char oldChar, char newChar){
    //判断替换和被替换的字符是否是同一个字符,如果是则不操作,返回this字符串。
    if(oldChar != newChar){
        int len = value.length;
        int i = -1;
        char[] val = value;

        //判断this字符串是否含有oldChar字符,如果没有,最后i=this字符串的长度
        while(++i < len){
            if(val[i] == oldChar){
                break;
            }
        }
        //如果this字符串包含oldChar字符
        if(i < len){
            char[] buf = new char[len];
            for(int j=0; j<i; j++){
                buf[j] = val[j];
            }
            //从第一个oldChar字符下标开始往后判断是否还有oldChar字符,有则替换
            while(i < len){
                char c = val[i];
                buf[i] = (c==oldChar)?newChar:c;
                i++;
            }
            return new String(buf,true);
        }
    }
    return this;
}

//将this字符串中出现的包含字符序列target的都替换成replacement
//target:目标字符,即this中要替换的字符
//replacement:替换成的字符串
public Stirng replace(CharSequence target, CharSequence replacement){
    return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
        this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
}

这个方法是的属性有:public公有。
参数:oldChar要被替换的字符
参数:formIndex替换的字符
返回值:String类型
返回值说明:返回一个将this字符串中的oldChar字符替换成newChar字符的字符串。

参数:target目标字符,即this中要替换的字符
参数:replacement替换的字符串,将符合的字符序列替换成replacement
返回值:String类型
返回值说明:返回一个将this字符串中的包含target字符替换成replacement字符序列的字符串。

方法注意:LITERAL的英文单词是literal,其意思为照字面的; 原义的。也就是说按照target的表面字符来作替换,而非正则表达式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值