由于1.8的rt.jar打开报错, 此番查看的是基于1.7
java.lang.String 是一个final类.
一, 接口
实现三个接口: Serializable, Comparable<String>, CharSequence
二, 属性
private final char[] value;
private int hash;
private static final long serialVersionUID = -6849794470754667710L;
private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];
public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(null);
private static final int HASHING_SEED = i;
private transient int hash32 = 0;
重要的属性有value和hash. 其中value是final,char数组存储的.
三, 构造器
java.lang.String的构造器比较多. 有个线程安全的构造器值得注意.
public String(StringBuffer paramStringBuffer)
{
synchronized (paramStringBuffer)
{
this.value = Arrays.copyOf(paramStringBuffer.getValue(), paramStringBuffer.length());
}
}
四,方法
1, equals
第一步:比较2个对象是否同一个对象,若是,直接返回true
第二步: 判断比较类型是否是java.lang.String,若否, 直接false
第三步: 判断字符长度是否相等, 若否, 直接false
第四步: 从第一个字符开始进行比较,若发现不相等,直接返回false.
细节: 当中判断用的是i++ !=0, 而不是我们平时j<this.value.length. 性能更佳.
public boolean equals(Object paramObject)
{
if (this == paramObject)
return true;
if ((paramObject instanceof String))
{
String str = (String)paramObject;
int i = this.value.length;
if (i == str.value.length)
{
char[] arrayOfChar1 = this.value;
char[] arrayOfChar2 = str.value;
for (int j = 0; i-- != 0; j++)
if (arrayOfChar1[j] != arrayOfChar2[j])
return false;
return true;
}
}
return false;
}
2, equalsIgnoreCase
直接比较地址是否相等,没有值比较.
public boolean equalsIgnoreCase(String paramString)
{
return this == paramString;
}
3, contentEquals
有2种, StringBuffer参数的调用CharSequence, 因为是线程安全的,所以加了synchronized.
boolean contentEquals(StringBuffer paramStringBuffer)
boolean contentEquals(CharSequence paramCharSequence)
3, compareTo
字符串集合进行排序的基础。 此方法比较2个字符串的大小。
第一步: 从2个字符串中取出最小字符长度。
第二步: 从0到最小字符串长度进行比较,若不相等,返回str1[i]-str[2]
第三步: 其它, 返回str1.length -str2.length
public int compareTo(String paramString)
{
int i = this.value.length;
int j = paramString.value.length;
int k = Math.min(i, j);
char[] arrayOfChar1 = this.value;
char[] arrayOfChar2 = paramString.value;
for (int m = 0; m < k; m++)
{
int n = arrayOfChar1[m];
int i1 = arrayOfChar2[m];
if (n != i1)
return n - i1;
}
return i - j;
}
4, startsWith
判断字符串是否以指定字符串开头.
boolean startsWith(String paramString): 默认从起始位置开始比较
boolean startsWith(String paramString, int paramInt): paramInt 开始比较的位置
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;
}
5, endsWith
提现了方法的重用性.
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
6, hashCode
当hash为0的时候,会进行计算,
计算方法是遍历每个字符进行 当前hash值*31+当前字符值 赋予hash
public int hashCode() {
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;
}
7, indexOf
找出目标字符串第一次出现的位置
public int indexOf(String str) {
return indexOf(str, 0);
}
public int indexOf(String str, int fromIndex) {
return indexOf(value, 0, value.length,
str.value, 0, str.value.length, 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];
int max = sourceOffset + (sourceCount - targetCount);
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;
}
8, substring
截取字符串,主要是调用构造器String(val,beginIndex,subLen),
而这个构造器用的是Arrats.copyOfRange(value, offset, offset+count);
substring(int benginIndex,int endIndex);
substring(int beginIndex);
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);
}
9, concat
拼接字符串,使用的是getChars方法
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
10, trim
去掉首尾空格. 实现简单,不用解释
public String trim() {
int len = value.length;
int st = 0;
char[] val = value; /* avoid getfield opcode */
while ((st < len) && (val[st] <= ' ')) {
st++;
}
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
}
11, replace
替换,没有使用正则表达式, replaceAll使用的是正则表达式
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) {
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;
}
12, getBytes
用的是StringCoding.encode方法,使用的是ISO-8859-1编码.
public byte[] getBytes() {
return StringCoding.encode(value, 0, value.length);
}
static byte[] encode(char[] ca, int off, int len) {
String csn = Charset.defaultCharset().name();
try {
// use charset name encode() variant which provides caching.
return encode(csn, ca, off, len);
} catch (UnsupportedEncodingException x) {
warnUnsupportedCharset(csn);
}
try {
return encode("ISO-8859-1", ca, off, len);
} catch (UnsupportedEncodingException x) {
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;