1. String的本质是一个 char数组,实现了CharSequence 接口,
/** The value is used for character storage. */ private final char value[];
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); }
a 算出最终的字符串长度 subLen,调用new String(value, beginIndex, subLen);
public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count < 0) { throw new StringIndexOutOfBoundsException(count); } // Note: offset or count might be near -1>>>1. if (offset > value.length - count) { throw new StringIndexOutOfBoundsException(offset + count); } this.value = Arrays.copyOfRange(value, offset, offset+count); } public static char[] copyOfRange(char[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); char[] copy = new char[newLength]; System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; }
调用System.arraycopy 复杂一个新的字符串返回 ,from 源的开始下标,Math.min(original.length – from, newLength) 新的字符串长度
3.replace
public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ //找到相等的那个字符的下标i while (++i < len) { if (val[i] == oldChar) { break; } } if (i < len) { //构造一个新的字符串数组buf char buf[] = new char[len]; //把相等下标之前的char值赋值 for (int j = 0; j < i; j++) { buf[j] = val[j]; } //下标i开始遍历 while (i < len) { char c = val[i]; //如果相等,进行替换 buf[i] = (c == oldChar) ? newChar : c; i++; } return new String(buf, true); } } return this; }
4.replaceAll 调用了正则表达式相关的类库
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
5.equals方法
public boolean equals(Object anObject) { //如果引用相等,返回true if (this == anObject) { return true; } //如果传入的对象为String类型 if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; // 遍历字符数组每个char, if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
6 compareTo方法
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; }
7 一个内部比较器 Comparator
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); private static class CaseInsensitiveComparator implements Comparator<String>, java.io.Serializable { // use serialVersionUID from JDK 1.2.2 for interoperability private static final long serialVersionUID = 8575799808933029326L; public int compare(String s1, String s2) { int n1 = s1.length(); int n2 = s2.length(); int min = Math.min(n1, n2); for (int i = 0; i < min; i++) { char c1 = s1.charAt(i); char c2 = s2.charAt(i); if (c1 != c2) { c1 = Character.toUpperCase(c1); c2 = Character.toUpperCase(c2); if (c1 != c2) { c1 = Character.toLowerCase(c1); c2 = Character.toLowerCase(c2); if (c1 != c2) { // No overflow because of numeric promotion return c1 - c2; } } } } return n1 - n2; } }