学习源码,从基层了解Java。
public final class implements java.io.Serializable, Comparable, CharSequence
String类是final类,无法被继承,实现了序列化,比较器以及字符序列接口。
private static final long serialVersionUID = -6849794470754667710L;
private static final ObjectStreamField[] serialPersistentFields =
new ObjectStreamField[0];
public static final Comparator CASE_INSENSITIVE_ORDER
= new CaseInsensitiveComparator();
private static class CaseInsensitiveComparator
implements Comparator, java.io.Serializable {
private static final long serialVersionUID = 8575799808933029326L;
public int compare(String s1, String s2)
private Object readResolve()
}
静态变量是序列值,比较器。
private final char value[];
private int hash;
成员变量是存储String值的value字符数组和hash值。
public String()
public String(String original)
public String(char value[])
public String(char value[], int offset, int count)
public String(int[] codePoints, int offset, int count)//Unicode
public String(byte bytes[], int offset, int length, String charsetName)//字节码解码为字符串
public String(byte bytes[], int offset, int length, Charset charset)//字节码解码为字符串
public String(byte bytes[], String charsetName)
public String(byte bytes[], Charset charset)
public String(byte bytes[], int offset, int length)
public String(byte bytes[])
public String(StringBuffer buffer)//同步
public String(StringBuilder builder)
String(char[] value, boolean share)
构造函数有字符数组、字节码数组,StringBuffer,StringBuilder转换为String字符串对象。
private static void checkBounds(byte[] bytes, int offset, int length)//检测数组越界抛出StringIndexOutOfBoundsException
static int indexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex)
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex)
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
String target, int fromIndex)
static int lastIndexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex)
public static String format(String format, Object... args)
public static String format(Locale l, String format, Object... args)
public static String valueOf(Object obj)
public static String valueOf(char data[])
public static String valueOf(char data[], int offset, int count)
public static String copyValueOf(char data[], int offset, int count)
public static String copyValueOf(char data[])
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)
静态函数包括返回匹配的第一个索引、最后一个索引、格式化、表示字符串。
String s = String.format("Hello %s,Welcome to my blog!", "jerry");
System.out.println(s);//Hello jerry,Welcome to my blog!
public int length()
public boolean isEmpty()
public char charAt(int index)
public int codePointAt(int index)//unicode
public int codePointBefore(int index)
public int codePointCount(int beginIndex, int endIndex)
public int offsetByCodePoints(int index, int codePointOffset)
void getChars(char dst[], int dstBegin)//字符串拷贝到从dstBegin索引开始的字符数组
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)//字符串拷贝到从dstBegin索引开始的字符数组
public byte[] getBytes(String charsetName)//编码
public byte[] getBytes(Charset charset)//编码
public byte[] getBytes()//编码
public boolean equals(Object anObject)
private boolean nonSyncContentEquals(AbstractStringBuilder sb)
public boolean contentEquals(CharSequence cs)
public boolean equalsIgnoreCase(String anotherString)//忽略字母大小
public int compareTo(String anotherString)
public int compareToIgnoreCase(String str)
public boolean regionMatches(int toffset, String other, int ooffset,
int len)
public boolean regionMatches(boolean ignoreCase, int toffset,
String other, int ooffset, int len)
public boolean startsWith(String prefix, int toffset)
public boolean startsWith(String prefix)
public boolean endsWith(String suffix)
public int hashCode()
public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
private int indexOfSupplementary(int ch, int fromIndex)
public int lastIndexOf(int ch)
public int lastIndexOf(int ch, int fromIndex)
private int lastIndexOfSupplementary(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
public CharSequence subSequence(int beginIndex, int endIndex)
public String concat(String str)
public String replace(char oldChar, char newChar)
public boolean contains(CharSequence s)
public String replaceFirst(String regex, String replacement)
public String replaceAll(String regex, String replacement)
public String replace(CharSequence target, CharSequence replacement)
public String[] split(String regex, int limit)
public String[] split(String regex)
public static String join(CharSequence delimiter, CharSequence... elements)
public static String join(CharSequence delimiter,
Iterable extends CharSequence> elements)
public String toLowerCase(Locale locale)
public String toLowerCase()
public String toUpperCase(Locale locale)
public String toUpperCase()
public String trim()
public String toString()
public char[] toCharArray()
public native String intern();
成员函数包括length()字符串长度,isEmpty()是否为空,charAt()索引字符,getChars()拷贝,getBytes()编码,equals()是否相等,compareTo()比较,startsWith()开始内容匹配,endsWith()结束内容匹配,hashCode()hash值,indexOf()匹配字符、字符串的首尾位置,substring()子序列,concat()连接,replace()取代,contains()包含,split()分割,join()字符数组转字符串,toLowerCase()转大小写,trim()去首尾空格,toString()字符串,toCharArray()转字符数组。
static int indexOf(char[] source, int sourceOffset, int sourceCount,
char[] target, int targetOffset, int targetCount,
int fromIndex):
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;
}
}
}
比较精髓的代码,while循环和for循环用的恰到好处,值得学习。