boolean startsWith(String prefix,int toffset)
public boolean startsWith(String prefix, int toffset) {//把prefix字符串和从toffset位置开始的用来比字符串的子字符串相比,相同返回true,不同返回false
char ta[] = value;
int to = toffset;
char pa[] = prefix.value;
int po = 0;
int pc = prefix.value.length;
// Note: toffset might be near -1>>>1.
if ((toffset < 0) || (toffset > value.length - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
public boolean endsWith(String suffix) {//用字符串suffix和用来比较的字符串的末尾的子字符相比,如果相同返回true,不同返回false
return startsWith(suffix, value.length - suffix.value.length);
}
replace部分解读
String replace(char oldChar,char newChar)
public String replace(char oldChar, char newChar) {
if (oldChar != newChar) {
int len = value.length;
int i = -1;
char[] val = value; /* avoid getfield opcode */
while (++i < len) {//保证i表示的是数组下标
if (val[i] == oldChar) {
break;
}
}
if (i < len) {
char buf[] = new char[len];
for (int j = 0; j < i; j++) {
buf[j] = val[j];
}
while (i < len) {
char c = val[i];
buf[i] = (c == oldChar) ? newChar : c;//循环判断该字符串中是否包含需要被替换的字符,并对其进行替换
i++;
}
return new String(buf, true);//最后生成的是一个新数组,并不是改变原来数组的指向
}
}
return this;//如果没有找到该字符串中有需要替换的字符,则返回原数组。(就是原指向地址)
}