String類的核心方法:
private final char[] value; // final修飾的char數(shù)組;
private int hashCode;
// trim方法
public String trim(){
? ? int start = 0;
? ? int end = this.value.length;
? ? char[] val=this.value;
? ? while( (start<end) && (val[start] <= ' ' )){
? ? ? ? start++;
????}
? ? while( start<end) && (val[end] <= ' '){
? ? ? ? end--;
? ? }
? ? return (start>0 || end<this.value.length) : subString(start,end) : this;
}
// subString方法
public String subString(start,end){
? ? if(start<0){
? ? ? ? throw new StringIndexOutOfBoundsException(start);
? ? }
? ? if(end>this.value.length){
? ???????throw new StringIndexOutOfBoundsException(end);
? ? }
? ? int sub=end-start;
? ? if(sub<0){
? ???????throw new StringIndexOutOfBoundsException(sub);
? ? }
? ? return (start==0)&&(end==this.value.length) ? this : new String(value,start,sub);
}
// startsWith方法
public boolean startsWith(String prefix,int offset){
? ? char[] source=this.value;
? ? int sl=this.value.length;
? ? char[] pre=prefix.value;
? ? int pl=prefix.value.length;
? ? if(offset<0 || offset>sl-pl){
? ? ? ? return false;
? ? ? }
? ? int index=0;
? ? while(--pl>=0){
? ? ? ? if(surce[offset++]!=pre[index++]){
? ? ? ? ? ? return false;
? ? ? ? }
? ? }
return true;
}