@Date:2018年08月13日16:55:06
- split
将输入字符串断开成字符串对象数组,断开边界由正则表达式确定
- 替换操作
replace/replaceAll/appendReplacement 通过正则表达式来替换结果
- compareTo(String anotherString)
基于每个字符的unicode编码值,比较两个字符串的字典顺序。小于零则
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
- startsWith(String prefix, int toffset)
通过while循环判断当前字符串是否是在指定偏移量以指定前缀开头
public boolean startsWith(String prefix, int toffset) {
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;
}
- indexOf
得到指定字符串从指定索引开始哪个索引值
- subString(int beginIndex , int endIndex)
返回一个字符串,该字符串是此字符串的子字符串。 子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符.
eg:String name = “zhuxiaolei” ; name = name.subString(4,8) = “iaol”;
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}