JDK源码-String
1,字符串,String类。
-1,字符串是常量,他们的值在创建后不能更改。字符串缓冲区支持可变的字符串。
-2,String重载了Java中的+操作。
-3,String对象是不可变的,你可以给一个String对象加任意多的别名。因为String对象具有只读特性,所以指向他的任何引用都不能改变它的值。
2,String类的成员变量
-1,value 存储字符串的char数组。
private final char value[];
-2,offset:存储使用的首元素index。
/** The offset is the first index of the storage that is used. */
private final int offset;
-3,count:String中存储的char数量。
/** The count is the number of characters in the String. */
private final int count;
-4,hash:字符串的hash值。
/** Cache the hash code for the string */
private int hash; // Default to 0
-5,
/**
* Class String is special cased within the Serialization Stream Protocol.
*
* A String instance is written initially into an ObjectOutputStream in the
* following format:
* <pre>
* <code>TC_STRING</code> (utf String)
* </pre>
* The String is written by method <code>DataOutput.writeUTF</code>.
* A new handle is generated to refer to all future references to the
* string instance within the stream.
*/
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
3,构造方法。
-1,String():构造空的字符串对象。默认内部数组长度为0
public String() {
this.offset = 0;
this.count = 0;
this.value = new char[0];
}
-2,String(String):使用指定字符串构造String,创建对应字符串的副本,因为字符串是不可变的,所以没必要使用此构造方法。
public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}
-3,String(char[] ):使用char数组构建String
public String(char value[]) {
int size = value.length;
this.offset = 0;
this.count = size;
this.value = Arrays.copyOf(value, size);
}
4,方法:
-1,length():返回字符串中字符数。
public int length() {
return count;
}
-2,isEmpty():判断字符串是否为空。
public boolean isEmpty() {
return count == 0;
}
-3,charAt(int):获取对应索引的字符。
public char charAt(int index) {
if ((index < 0) || (index >= count)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index + offset];
}
-4,equalsIgnoreCase(String):忽略大小写比较两字符串差异。
public boolean equalsIgnoreCase(String anotherString) {
return (this == anotherString) ? true :
(anotherString != null) && (anotherString.count == count) &&
regionMatches(true, 0, anotherString, 0, count);//调用此方法
}
判断;两字符串中从开始位置到固定长度的字符是否相等。
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len) {
char ta[] = value;
int to = offset + toffset;
char pa[] = other.value;
int po = other.offset + ooffset;
// Note: toffset, ooffset, or len might be near -1>>>1.
if ((ooffset < 0) || (toffset < 0) || (toffset > (long)count - len) ||
(ooffset > (long)other.count - len)) {
return false;
}
while (len-- > 0) {
char c1 = ta[to++];
char c2 = pa[po++];
if (c1 == c2) {
continue;
}
if (ignoreCase) {
// If characters don't match but case may be ignored,
// try converting both characters to uppercase.
// If the results match, then the comparison scan should
// continue.
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// Unfortunately, conversion to uppercase does not work properly
// for the Georgian alphabet, which has strange rules about case
// conversion. So we need to make one last check before
// exiting.
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}
}
return false;
}
return true;
}
-5,startWith(String, int):判断字符串是不是从某索引位置以某字符串开始。
public boolean startsWith(String prefix, int toffset) {
char ta[] = value;
int to = offset + toffset;//索引开始位置
char pa[] = prefix.value;
int po = prefix.offset;//开始位置
int pc = prefix.count;//总长度
// Note: toffset might be near -1>>>1.
if ((toffset < 0) || (toffset > count - pc)) {
return false;
}
while (--pc >= 0) {
if (ta[to++] != pa[po++]) {
return false;
}
}
return true;
}
-6,endWith(String):以某字符串结束。
public boolean endsWith(String suffix) {
return startsWith(suffix, count - suffix.count);//获取开始比较位置,然后调用开始比较的方法,开始比较。
}
-7,startWith(String):判断字符串是否以某字符串开始。
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
欢迎转载,但转载请注明原文链接[博客园: http://www.cnblogs.com/jingLongJun/]
[CSDN博客:http://blog.csdn.net/mergades]。
如相关博文涉及到版权问题,请联系本人。
[CSDN博客:http://blog.csdn.net/mergades]。
如相关博文涉及到版权问题,请联系本人。