publicbooleancontentEquals(CharSequence cs) {
// Argument is a StringBuffer, StringBuilderif (cs instanceof AbstractStringBuilder) {
if (cs instanceof StringBuffer) {
synchronized(cs) {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder)cs);
}
}
// Argument is a Stringif (cs instanceof String) {
return equals(cs);
}
// Argument is a generic CharSequencechar v1[] = value;
int n = v1.length;
if (n != cs.length()) {
returnfalse;
}
for (int i = 0; i < n; i++) {
if (v1[i] != cs.charAt(i)) {
returnfalse;
}
}
returntrue;
}
compareTo
//按字典序比较publicintcompareTo(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
//找value从toffest开始,与prefix是否相等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)) {
returnfalse;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
returnfalse;
}
}
returntrue;
}
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
hashCode
//string的hashcode的算法,依旧用的31,这里初始是0,hashmap里初始是1publicinthashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
indexOf
publicintindexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} elseif (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return indexOfSupplementary(ch, fromIndex);
}
}
//为何参数这么多
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex) {
if (fromIndex >= sourceCount) {
return (targetCount == 0 ? sourceCount : -1);
}
if (fromIndex < 0) {
fromIndex = 0;
}
if (targetCount == 0) {
return fromIndex;
}
char first = target[targetOffset];
intmax = sourceOffset + (sourceCount - targetCount);
//暴力找,居然不是kmp什么的for (int i = sourceOffset + fromIndex; i <= max; i++) {
/* Look for first character. */if (source[i] != first) {
while (++i <= max && source[i] != first);
}
/* Found first character, now look at the rest of v2 */if (i <= max) {
int j = i + 1;
int end = j + targetCount - 1;
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
if (j == end) {
/* Found whole string. */return i - sourceOffset;
}
}
}
return -1;
}
subString
// 当子串刚好是本串时,就返回this,这也是String的一大思想,不可变,对于一个确定的字符串,可以用同一个对象表示public String substring(int beginIndex) {
if (beginIndex < 0) {
thrownew StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
thrownew StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
thrownew StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
thrownew StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
thrownew StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
concat
//讲str添加到value的后面,这里也给出了一种字符串相加的方法public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
returnthis;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
returnnew String(buf, true);
}
replace
//将字符串中所有的oldChar替换为newCharpublic 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) {
if (val[i] == oldChar) {
break;
}
}
//如果本串中没有oldChar,直接返回本串if (i < len) {
char buf[] = newchar[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++;
}
returnnew String(buf, true);
}
}
returnthis;
}
toLowerCase
//locale,居然可以选语言!真厉害了!当然,默认是英语的public String toLowerCase(Locale locale) {
if (locale == null) {
thrownew NullPointerException();
}
int firstUpper;
final int len = value.length;
/* Now check if there are any characters that need to be changed. */
scan: {
for (firstUpper = 0 ; firstUpper < len; ) {
char c = value[firstUpper];
if ((c >= Character.MIN_HIGH_SURROGATE)
&& (c <= Character.MAX_HIGH_SURROGATE)) {
int supplChar = codePointAt(firstUpper);
if (supplChar != Character.toLowerCase(supplChar)) {
break scan;
}
firstUpper += Character.charCount(supplChar);
} else {
if (c != Character.toLowerCase(c)) {
break scan;
}
firstUpper++;
}
}
returnthis;
}
char[] result = newchar[len];
int resultOffset = 0; /* result may grow, so i+resultOffset
* is the write location in result *//* Just copy the first few lowerCase characters. */
System.arraycopy(value, 0, result, 0, firstUpper);
String lang = locale.getLanguage();
boolean localeDependent =
(lang == "tr" || lang == "az" || lang == "lt");
char[] lowerCharArray;
int lowerChar;
int srcChar;
int srcCount;
for (int i = firstUpper; i < len; i += srcCount) {
srcChar = (int)value[i];
if ((char)srcChar >= Character.MIN_HIGH_SURROGATE
&& (char)srcChar <= Character.MAX_HIGH_SURROGATE) {
srcChar = codePointAt(i);
srcCount = Character.charCount(srcChar);
} else {
srcCount = 1;
}
if (localeDependent ||
srcChar == '\u03A3' || // GREEK CAPITAL LETTER SIGMA
srcChar == '\u0130') { // LATIN CAPITAL LETTER I WITH DOT ABOVE
lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);
} else {
lowerChar = Character.toLowerCase(srcChar);
}
if ((lowerChar == Character.ERROR)
|| (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {
if (lowerChar == Character.ERROR) {
lowerCharArray =
ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);
} elseif (srcCount == 2) {
resultOffset += Character.toChars(lowerChar, result, i + resultOffset) - srcCount;
continue;
} else {
lowerCharArray = Character.toChars(lowerChar);
}
/* Grow result if needed */int mapLen = lowerCharArray.length;
if (mapLen > srcCount) {
char[] result2 = newchar[result.length + mapLen - srcCount];
System.arraycopy(result, 0, result2, 0, i + resultOffset);
result = result2;
}
for (int x = 0; x < mapLen; ++x) {
result[i + resultOffset + x] = lowerCharArray[x];
}
resultOffset += (mapLen - srcCount);
} else {
result[i + resultOffset] = (char)lowerChar;
}
}
returnnew String(result, 0, len + resultOffset);
}
//默认英语public String toLowerCase() {
returntoLowerCase(Locale.getDefault());
}
//toUpperCase基本同理,就不贴了
//有点儿复杂public String[] split(String regex, int limit) {
/* fastpath if the regex is a
(1)one-char String and this character is not one of the
RegEx's meta characters ".$|()[{^?*+\\", or
(2)two-char String and the first char is the backslash and
the second is not the ascii digit or ascii letter.
*/char ch = 0;
if (((regex.value.length == 1 &&
".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
(regex.length() == 2 &&
regex.charAt(0) == '\\' &&
(((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
((ch-'a')|('z'-ch)) < 0 &&
((ch-'A')|('Z'-ch)) < 0)) &&
(ch < Character.MIN_HIGH_SURROGATE ||
ch > Character.MAX_LOW_SURROGATE))
{
int off = 0;
int next = 0;
boolean limited = limit > 0;
ArrayList<String> list = new ArrayList<>();
while ((next = indexOf(ch, off)) != -1) {
if (!limited || list.size() < limit - 1) {
list.add(substring(off, next));
off = next + 1;
} else { // last one//assert (list.size() == limit - 1);list.add(substring(off, value.length));
off = value.length;
break;
}
}
// If no match was found, return thisif (off == 0)
returnnew String[]{this};
// Add remaining segmentif (!limited || list.size() < limit)
list.add(substring(off, value.length));
// Construct resultint resultSize = list.size();
if (limit == 0) {
while (resultSize > 0 && list.get(resultSize - 1).length() == 0) {
resultSize--;
}
}
String[] result = new String[resultSize];
returnlist.subList(0, resultSize).toArray(result);
}
return Pattern.compile(regex).split(this, limit);
}
getBytes
publicvoidgetBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {
if (srcBegin < 0) {
thrownew StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
thrownew StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
thrownew StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
Objects.requireNonNull(dst);
//为何要重新赋值?int j = dstBegin;
int n = srcEnd;
int i = srcBegin;
char[] val = value; /* avoid getfield opcode */while (i < n) {
dst[j++] = (byte)val[i++];
}
}
publicbyte[] getBytes(String charsetName)
throws UnsupportedEncodingException {
if (charsetName == null) thrownew NullPointerException();
return StringCoding.encode(charsetName, value, 0, value.length);
}
publicbyte[] getBytes(Charset charset) {
if (charset == null) thrownew NullPointerException();
return StringCoding.encode(charset, value, 0, value.length);
}
publicbyte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}