05 java.lang.String

来源:互联网 发布:淘宝店家订单管理导出 编辑:程序博客网 时间:2024/04/30 13:24

String

                                                            2015.01.14&15                                                            By 970655147

这个类, 相信是java中用到的最多的一个类了吧, 任何地方都有他的踪迹, 比如你要打印数据到控制台, 或者从网页中抓取数据, 等等
java 中的恒常类之一 [恒常类:String、Integer、Boolean、Float、Double、Char、Byte], 这个类是非常重要的, 非常有必要详详细细的了解一下
当然 如果使用不当的话, 开销是非常大的, 如果需要动态拼接字符串, 建议采用StringBuilder/ StringBuffer

start ->

声明

, 大家可以看看注释

/** * The <code>String</code> class represents character strings. All * string literals in Java programs, such as <code>"abc"</code>, are * implemented as instances of this class. * <p> * Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example: * <p><blockquote><pre> *     String str = "abc"; * </pre></blockquote><p> * is equivalent to: * <p><blockquote><pre> *     char data[] = {'a', 'b', 'c'}; *     String str = new String(data); * </pre></blockquote><p> * Here are some more examples of how strings can be used: * <p><blockquote><pre> *     System.out.println("abc"); *     String cde = "cde"; *     System.out.println("abc" + cde); *     String c = "abc".substring(2,3); *     String d = cde.substring(1, 2); * </pre></blockquote> * <p> * The class <code>String</code> includes methods for examining * individual characters of the sequence, for comparing strings, for * searching strings, for extracting substrings, and for creating a * copy of a string with all characters translated to uppercase or to * lowercase. Case mapping is based on the Unicode Standard version * specified by the {@link java.lang.Character Character} class. * <p> * The Java language provides special support for the string * concatenation operator (&nbsp;+&nbsp;), and for conversion of * other objects to strings. String concatenation is implemented * through the <code>StringBuilder</code>(or <code>StringBuffer</code>) * class and its <code>append</code> method. * String conversions are implemented through the method * <code>toString</code>, defined by <code>Object</code> and * inherited by all classes in Java. For additional information on * string concatenation and conversion, see Gosling, Joy, and Steele, * <i>The Java Language Specification</i>. * * <p> Unless otherwise noted, passing a <tt>null</tt> argument to a constructor * or method in this class will cause a {@link NullPointerException} to be * thrown. * * <p>A <code>String</code> represents a string in the UTF-16 format * in which <em>supplementary characters</em> are represented by <em>surrogate * pairs</em> (see the section <a href="Character.html#unicode">Unicode * Character Representations</a> in the <code>Character</code> class for * more information). * Index values refer to <code>char</code> code units, so a supplementary * character uses two positions in a <code>String</code>. * <p>The <code>String</code> class provides methods for dealing with * Unicode code points (i.e., characters), in addition to those for * dealing with Unicode code units (i.e., <code>char</code> values). * * @author  Lee Boynton * @author  Arthur van Hoff * @author  Martin Buchholz * @author  Ulf Zibis * @see     java.lang.Object#toString() * @see     java.lang.StringBuffer * @see     java.lang.StringBuilder * @see     java.nio.charset.Charset * @since   JDK1.0 */public final class String implements java.io.Serializable, Comparable<String>, CharSequence

String. 属性

    // 存储字符串的字符    private final char value[];    // hashCode    private int hash; // Default to 0    // 大小写敏感的Comparator    public static final Comparator<String> CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator();

String. length()/ isEmpty()

    public int length() {        return value.length;    }    public boolean isEmpty() {        return value.length == 0;    }

String. charAt(int index)

public char charAt(int index) {    // checkBounds        if ((index < 0) || (index >= value.length)) {            throw new StringIndexOutOfBoundsException(index);        }        // 返回指定索引的字符        return value[index];    }

String. codePointAt(int index)

// 虽然没看懂但是好像返回是a的index处字符的unicode编码    public int codePointAt(int index) {        if ((index < 0) || (index >= value.length)) {            throw new StringIndexOutOfBoundsException(index);        }        return Character.codePointAtImpl(value, index, value.length);    }// 至于中间的处理流程应该是针对什么特殊字符的吧    static int codePointAtImpl(char[] a, int index, int limit) {        char c1 = a[index++];        if (isHighSurrogate(c1)) {            if (index < limit) {                char c2 = a[index];                if (isLowSurrogate(c2)) {                    return toCodePoint(c1, c2);                }            }        }        return c1;    }

String. codePointBefore(int index)

    // 返回指定索引的之前一个字符的unicode码    public int codePointBefore(int index) {        int i = index - 1;        if ((i < 0) || (i >= value.length)) {            throw new StringIndexOutOfBoundsException(index);        }        return Character.codePointBeforeImpl(value, index, 0);    }    static int codePointBeforeImpl(char[] a, int index, int start) {        char c2 = a[--index];        if (isLowSurrogate(c2)) {            if (index > start) {                char c1 = a[--index];                if (isHighSurrogate(c1)) {                    return toCodePoint(c1, c2);                }            }        }        return c2;    }

String. codePointCount(int beginIndex, int endIndex)

    // 返回能用codePoint表示的字符的个数    public int codePointCount(int beginIndex, int endIndex) {        if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex) {            throw new IndexOutOfBoundsException();        }        return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);    }    static int codePointCountImpl(char[] a, int offset, int count) {        int endIndex = offset + count;        int n = count;        for (int i = offset; i < endIndex; ) {            if (isHighSurrogate(a[i++]) && i < endIndex &&                isLowSurrogate(a[i])) {                n--;                i++;            }        }        return n;    }

String. offsetByCodePoints(int index, int codePointOffset)

    //返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。    public int offsetByCodePoints(int index, int codePointOffset) {        if (index < 0 || index > value.length) {            throw new IndexOutOfBoundsException();        }        return Character.offsetByCodePointsImpl(value, 0, value.length,                index, codePointOffset);    }    static int offsetByCodePointsImpl(char[]a, int start, int count, int index, int codePointOffset) {        int x = index;        if (codePointOffset >= 0) {            int limit = start + count;            int i;            for (i = 0; x < limit && i < codePointOffset; i++) {                if (isHighSurrogate(a[x++]) && x < limit &&                    isLowSurrogate(a[x])) {                    x++;                }            }            if (i < codePointOffset) {                throw new IndexOutOfBoundsException();            }        } else {            int i;            for (i = codePointOffset; x > start && i < 0; i++) {                if (isLowSurrogate(a[--x]) && x > start &&                    isHighSurrogate(a[x-1])) {                    x--;                }            }            if (i < 0) {                throw new IndexOutOfBoundsException();            }        }        return x;    }// 备注: 一个完整的Unicode字符叫代码点/CodePoint,而一个Java char 叫代码单元code unit;string对象以UTF-16保存Unicode字符,需要用2个字符表示一个超大字符集汉字,这种表示方式为Sruuogate,第一个字符叫Surrogate High,第二个就是Surrogate Low判断一个char是否是Surrogate区的字符,用Character的isHighSurrogate()/isLowSurrogate()方法。从两个Surrogate High/Low字符,返回一个完整的Unicode CodePoint用Character.toCodePoint()/codePointAt()一个Code Point,可能需要一个也可能需要两个char表示,因此不能直接使用CharSequence.length()方法返回一个字符串到底有多少个汉字,而需要用String.codePointCount()/Character.codePointCount()要定位字符串中的第N个字符,不能直接将n作为偏移量,而需要从字符串头部依次遍历得到,需要String.offsetByCodePoints()从字符串的当前字符,找到上一个字符,不能直接用offset实现,而需要String.codePointBefore(),或String.offsetByCodePoints()从当前字符,找下一个字符,需要判断当前CodePoint的长度,再计算得到String.offsetByCodePoints()看了之后还是不太明白String str("web开发");int len = str.length();System.out.println();输出结果为5;改为用int len = str.codePointCount(0,str.length());输出结果同样为5定位一个字符用char cp = str.charAt(4);输出为“发”;若用int index = str.offsetByCodePoints(0,4);       int cp = str.codePointAt(index);输出为21457;什么意思??原来是获取字符串中指定位置的字符的UNICODE值,其值等同于(int)charAt(i)jdk1.5中,string类新方法介绍:http://www.javayou.com/html/diary/showlog.vm?sid=2&log_id=557

String. getChars(char dst[], int dstBegin)

void getChars(char dst[], int dstBegin) {    // 拷贝value到dst[dstBegin]开始处        System.arraycopy(value, 0, dst, dstBegin, value.length);    }public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {    // checkBounds        if (srcBegin < 0) {            throw new StringIndexOutOfBoundsException(srcBegin);        }        if (srcEnd > value.length) {            throw new StringIndexOutOfBoundsException(srcEnd);        }        if (srcBegin > srcEnd) {            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);        }        //         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);    }    public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

String. getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin)

// 获取当前字符对象的srcBegin到srcEnd的字符 复制到dst的dstBegin出    @Deprecated    public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {        if (srcBegin < 0) {            throw new StringIndexOutOfBoundsException(srcBegin);        }        if (srcEnd > value.length) {            throw new StringIndexOutOfBoundsException(srcEnd);        }        if (srcBegin > srcEnd) {            throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);        }        int j = dstBegin;        int n = srcEnd;        int i = srcBegin;        char[] val = value;   /* avoid getfield opcode */        // 复制字节[(byte)ch]        while (i < n) {            dst[j++] = (byte)val[i++];        }    }    // 以指定的编码返回该表示String的字节数组    public byte[] getBytes(String charsetName)            throws UnsupportedEncodingException {        if (charsetName == null) throw new NullPointerException();        return StringCoding.encode(charsetName, value, 0, value.length);    }// 以指定的编码返回该表示String的字节数组    public byte[] getBytes(Charset charset) {        if (charset == null) throw new NullPointerException();        return StringCoding.encode(charset, value, 0, value.length);    }    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;        }    }    // 获取默认的字符集    public static Charset defaultCharset() {        if (defaultCharset == null) {            synchronized (Charset.class) {                String csn = AccessController.doPrivileged(                    new GetPropertyAction("file.encoding"));                Charset cs = lookup(csn);                if (cs != null)                    defaultCharset = cs;                else                    defaultCharset = forName("UTF-8");            }        }        return defaultCharset;    }

String. equals(Object anObject)

public boolean equals(Object anObject) {    // 如果两个引用相等 直接判定为二者相等        if (this == anObject) {            return true;        }        // RTTI        if (anObject instanceof String) {            String anotherString = (String) anObject;            int n = value.length;            // 用字符串长度过滤一次            if (n == anotherString.value.length) {                char v1[] = value;   /* avoid getfield opcode */                char v2[] = anotherString.value;                int i = 0;                    // 依次比较每一个字符                while (n-- != 0) {                    if (v1[i] != v2[i])                            return false;                    i++;                }                return true;            }        }        return false;}

String. contentEquals(StringBuffer sb)

    // 比较当前字符串和sb的内容是否相同    public boolean contentEquals(StringBuffer sb) {        synchronized (sb) {            return contentEquals((CharSequence) sb);        }    }public boolean contentEquals(CharSequence cs) {    // 用字符串长度过滤一次        if (value.length != cs.length())            return false;        // Argument is a StringBuffer, StringBuilder         // 如果cs是StringBuilder,StringBuffer        if (cs instanceof AbstractStringBuilder) {            char v1[] = value;            char v2[] = ((AbstractStringBuilder) cs).getValue();            int i = 0;            int n = value.length;            // 依次比较每一个字符            while (n-- != 0) {                if (v1[i] != v2[i])                    return false;                i++;            }            return true;        }         // 否则参数是字符串        // Argument is a String        if (cs.equals(this))            return true;        // Argument is a generic CharSequence        char v1[] = value;        int i = 0;        int n = value.length;        // 依次比较每一个字符        while (n-- != 0) {            if (v1[i] != cs.charAt(i))                return false;            i++;        }        return true;    }

String. equalsIgnoreCase(String anotherString)

    public boolean equalsIgnoreCase(String anotherString) {        return (this == anotherString) ? true                : (anotherString != null)                // 逻辑与 可以当if                && (anotherString.value.length == value.length)                && regionMatches(true, 0, anotherString, 0, value.length);    }    public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) {        char ta[] = value;        int to = toffset;        char pa[] = other.value;        int po = ooffset;        // Note: toffset, ooffset, or len might be near -1>>>1.        // checkBounds        if ((ooffset < 0) || (toffset < 0)                || (toffset > (long)value.length - len)                || (ooffset > (long)other.value.length - 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.                // 有些Georgian alphabet 它们的大小写转换是不规则的,所以我们需要在做一次检测                if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {                    continue;                }            }            // 如果有任意一个字符不相同[ignoreCase]  返回false            return false;        }        return true;    }

String. compareTo(String anotherString)

    public int compareTo(String anotherString) {        int len1 = value.length;        int len2 = anotherString.value.length;        // 取本身字符串的长度和比较字符串的长度中较小者        int lim = Math.min(len1, len2);        // avoid getField opCode        char v1[] = value;        char v2[] = anotherString.value;        int k = 0;        // 一次从第一个字符开始比较        while (k < lim) {            char c1 = v1[k];            char c2 = v2[k];            // 如果value[k] != anotherString.value[k]则可以出结果了            if (c1 != c2) {                return c1 - c2;            }            k++;        }        // 如果在lim长度内 两个字符串都相同 则判定长度较长的较大        return len1 - len2;    }

String. compareToIgnoreCase(String str)

    public int compareToIgnoreCase(String str) {        return CASE_INSENSITIVE_ORDER.compare(this, str);    }    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);                // 对于两个字符串的前min个字符 先比较字符串本身,在比较转换成大写字符的字符 在比较转换成小写字母的字符,如果不相等 则返回value[i] – anotherString.value[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;                        }                    }                }            }            // 如果在min长度内 两个字符串都相同 则判定长度较长的较大            return n1 - n2;        }    }

String. regionMatches(int toffset, String other, int ooffset, int len)

    // 比较本身字符串从tooffset和other从ooffset开始len个长度的字符串的是否相等    public boolean regionMatches(int toffset, String other, int ooffset, int len) {        char ta[] = value;        int to = toffset;        char pa[] = other.value;        int po = ooffset;        // Note: toffset, ooffset, or len might be near -1>>>1.        if ((ooffset < 0) || (toffset < 0)                || (toffset > (long)value.length - len)                || (ooffset > (long)other.value.length - len)) {            return false;        }        // 遍历这len个字符 如果有一个字符不相等 则返回false        while (len-- > 0) {            if (ta[to++] != pa[po++]) {                return false;            }        }        return true;    }

String. startsWith()/ endsWith()

    public boolean startsWith(String prefix) {    // startsWith默认的本身字符串的偏移为0        return startsWith(prefix, 0);    }    public boolean endsWith(String suffix) {    //本身字符串的偏移为 value.length - suffix.value.length)    // 这个实现的还真是巧妙..        return startsWith(suffix, value.length - suffix.value.length);}    // 在当前字符串的tofffset处是否已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)) {            return false;        }        // 比较本身字符串的offset起prefix.length()和prefix是否相等        while (--pc >= 0) {            if (ta[to++] != pa[po++]) {                return false;            }        }        return true;    }

String. hashCode()

    public int hashCode() {        int h = hash;        if (h == 0 && value.length > 0) {            char val[] = value;            // 选取31[质数]作为hash种子            for (int i = 0; i < value.length; i++) {                h = 31 * h + val[i];            }            hash = h;        }        return h;    }

String. indexOf(int ch)

    public int indexOf(int ch) {        return indexOf(ch, 0);    }    public int indexOf(int ch, int fromIndex) {        final int max = value.length;        if (fromIndex < 0) {            fromIndex = 0;        } else if (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;             // 从fromIndex处开始查找 ch            for (int i = fromIndex; i < max; i++) {                if (value[i] == ch) {                    return i;                }            }            return -1;        } else {            return indexOfSupplementary(ch, fromIndex);        }    }    /**     * Handles (rare) calls of indexOf with a supplementary character.     */    // 增补字符的索引    private int indexOfSupplementary(int ch, int fromIndex) {        if (Character.isValidCodePoint(ch)) {            final char[] value = this.value;            final char hi = Character.highSurrogate(ch);            final char lo = Character.lowSurrogate(ch);            final int max = value.length - 1;            for (int i = fromIndex; i < max; i++) {                if (value[i] == hi && value[i + 1] == lo) {                    return i;                }            }        }        return -1;    }                                                                                        2015.01.14

String. lastIndexOf(int ch)

    public int lastIndexOf(int ch) {        return lastIndexOf(ch, value.length - 1);    }    public int lastIndexOf(int ch, int fromIndex) {        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;             // 防止fromIndex越界            int i = Math.min(fromIndex, value.length - 1);             // 从后面依次向前查找            for (; i >= 0; i--) {                if (value[i] == ch) {                    return i;                }            }            return -1;        } else {            return lastIndexOfSupplementary(ch, fromIndex);        }    }    // 增补字符的索引    private int lastIndexOfSupplementary(int ch, int fromIndex) {        if (Character.isValidCodePoint(ch)) {            final char[] value = this.value;            char hi = Character.highSurrogate(ch);            char lo = Character.lowSurrogate(ch);            int i = Math.min(fromIndex, value.length - 2);            for (; i >= 0; i--) {                if (value[i] == hi && value[i + 1] == lo) {                    return i;                }            }        }        return -1;}

String. indexOf(String str)

    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);    }    // 从源字符串指定偏移起的指定长度的子字符数组的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;    }

String. lastIndexOf(String str)

    public int lastIndexOf(String str) {        return lastIndexOf(str, value.length);    }    public int lastIndexOf(String str, int fromIndex) {        return lastIndexOf(value, 0, value.length,                str.value, 0, str.value.length, fromIndex);    }    static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) {        /*         * Check arguments; return immediately where possible. For         * consistency, don't check for null str.         */        int rightIndex = sourceCount - targetCount;        if (fromIndex < 0) {            return -1;        }        // 如果fromIndex大于两个字符数组的长度差 令fromIndex等于两个字符数组的长度差 只匹配源字符数组中能与目标字符数组匹配的最后一组数据        if (fromIndex > rightIndex) {            fromIndex = rightIndex;        }        /* Empty string always matches. */        // 空字符串任意地方都能匹配        if (targetCount == 0) {            return fromIndex;        }        int strLastIndex = targetOffset + targetCount - 1;        char strLastChar = target[strLastIndex];        // 源字符串中能匹配指定偏移,长度的目标字符数组的最小索引位置        int min = sourceOffset + targetCount - 1;        int i = min + fromIndex;        // 从min+fromIndex处开始查找        startSearchForLastChar:        while (true) {            while (i >= min && source[i] != strLastChar) {                i--;            }            if (i < min) {                return -1;            }            int j = i - 1;            int start = j - (targetCount - 1);            int k = strLastIndex - 1;            while (j > start) {                if (source[j--] != target[k--]) {                    i--;                    continue startSearchForLastChar;                }            }            // 能找到一个与目标字符串匹配的子串            return start - sourceOffset + 1;        }    }

String. 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);        }        // 如果beginIndex==0 直接返回自己的索引        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);    }    // 返回指定 开始/结束 索引的子字符串    public String substring(int beginIndex, int endIndex) {        if (beginIndex < 0) {            throw new StringIndexOutOfBoundsException(beginIndex);        }        if (endIndex > value.length) {            throw new StringIndexOutOfBoundsException(endIndex);        }        int subLen = endIndex - beginIndex;        if (subLen < 0) {            throw new StringIndexOutOfBoundsException(subLen);        }        // 如果beginIndex==0 并且 endIndex==value.length 返回自己的索引        return ((beginIndex == 0) && (endIndex == value.length)) ? this                : new String(value, beginIndex, subLen);    }public CharSequence subSequence(int beginIndex, int endIndex) {    // String implement CharSequence        return this.substring(beginIndex, endIndex);    }

String. concat(String str)

    public String concat(String str) {        int otherLen = str.length();        if (otherLen == 0) {            return this;        }        int len = value.length;        // 将value的值拷贝的新生成的字符数组中...        char buf[] = Arrays.copyOf(value, len + otherLen);        // 在将str的字符拷贝到buf[len]中        str.getChars(buf, len);        // 构造字符串        return new String(buf, true);    }    public static char[] copyOf(char[] original, int newLength) {        char[] copy = new char[newLength];        // 将original中的数据拷贝Math.min(original.length, newLength)个到copy  并返回copy数组        System.arraycopy(original, 0, copy, 0,                         Math.min(original.length, newLength));        return copy;    }    void getChars(char dst[], int dstBegin) {    // 将源字符数组的所有字符拷贝到目标字符数组,偏移为dstBegin        System.arraycopy(value, 0, dst, dstBegin, value.length);    }

String. replace(char oldChar, char newChar)

public String replace(char oldChar, char newChar) {    // 如果oldChar==newChar 直接返回this        if (oldChar != newChar) {            int len = value.length;            int i = -1;            char[] val = value; /* avoid getfield opcode */            // 首先遍历一次value 查找是否至少存在一个char为oldChar            while (++i < len) {                if (val[i] == oldChar) {                    break;                }            }            // 如果至少存在一个oldChar  新建一个字符数组, value是final并且是引用类型  那么按理说可以修改value中的值  但是为什么设计的时候不能修改呢?? 便于网络传输吗            if (i < len) {                char buf[] = new char[len];//填充进value的值                for (int j = 0; j < i; j++) {                    buf[j] = val[j];                }                // 遍历buf 将旧的字符替换为新的字符                while (i < len) {                    char c = val[i];                    buf[i] = (c == oldChar) ? newChar : c;                    i++;                }                // 构造新的字符串                return new String(buf, true);            }        }        return this;    }

String. matches(String regex)

    public boolean matches(String regex) {        return Pattern.matches(regex, this);    }    public static boolean matches(String regex, CharSequence input) {    // compile 正则表达式        Pattern p = Pattern.compile(regex);    // 根据输入字符序列 返回一个比较器        Matcher m = p.matcher(input);        // 计算字符序列是否匹配指定的正则表达式        return m.matches();    }

String. contains(CharSequence s)

public boolean contains(CharSequence s) {    // 判定给定的字符序列在 本身字符串中的索引是否不为-1        return indexOf(s.toString()) > -1;    }

String. replaceFirst(String regex, String replacement)

// 替换正则表达式匹配的第一个字符串 将其替换为replacementpublic String replaceFirst(String regex, String replacement) {        return Pattern.compile(regex).matcher(this).replaceFirst(replacement);    }

String. replaceAll(String regex, String replacement)

// 替换正则表达式匹配的替换所有匹配指定正则表达式的子字符串replacement    public String replaceAll(String regex, String replacement) {        return Pattern.compile(regex).matcher(this).replaceAll(replacement);    }

String. replace(CharSequence target, CharSequence replacement)

// 将源字符串中匹配target的字符串 均替换为replacement    public String replace(CharSequence target, CharSequence replacement) {        return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(                this).replaceAll(Matcher.quoteReplacement(replacement.toString()));    }

String. split(String regex)

    public String[] split(String regex) {        return split(regex, 0);    }    // 太复杂先不看..    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;        // 如果regex是一个普通字符串..        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<>();            // 依次找到这个匹配字符串之前的偏移 然后将其加入list            while ((next = indexOf(ch, off)) != -1) {                // 并非最后一个   从这里来看limit应该是限制分割后的数组的长度为多少个                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 this            // 如果没有匹配到split字符串            if (off == 0)                return new String[]{this};            // Add remaining segment            if (!limited || list.size() < limit)                list.add(substring(off, value.length));            // Construct result            int resultSize = list.size();            // 如果limit为0[不限制] 计算从list的后边查看去掉空字符串的size            if (limit == 0)                while (resultSize > 0 && list.get(resultSize - 1).length() == 0)                    resultSize--;            String[] result = new String[resultSize];            // 构造分割后的字符数组            return list.subList(0, resultSize).toArray(result);        }        // 如果regex符合正则表达式规则        return Pattern.compile(regex).split(this, limit);    }

String. toLowerCase()

    public String toLowerCase() {        return toLowerCase(Locale.getDefault());    }    public String toLowerCase(Locale locale) {        if (locale == null) {            throw new 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);                    // 找到大写字符 break scan                    if (supplChar != Character.toLowerCase(supplChar)) {                        break scan;                    }                    firstUpper += Character.charCount(supplChar);                } else {                    // 找到大写字符 break scan                    if (c != Character.toLowerCase(c)) {                        break scan;                    }                    firstUpper++;                }            }            // 没有大写字符 返回this            return this;        }        char[] result = new char[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                lowerChar = ConditionalSpecialCasing.toLowerCaseEx(this, i, locale);            } else if (srcChar == '\u0130') { // LATIN CAPITAL LETTER I DOT                lowerChar = Character.ERROR;            } else {                lowerChar = Character.toLowerCase(srcChar);            }            if ((lowerChar == Character.ERROR)                    || (lowerChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {                if (lowerChar == Character.ERROR) {                    if (!localeDependent && srcChar == '\u0130') {                        lowerCharArray =                                ConditionalSpecialCasing.toLowerCaseCharArray(this, i, Locale.ENGLISH);                    } else {                        lowerCharArray =                                ConditionalSpecialCasing.toLowerCaseCharArray(this, i, locale);                    }                } else if (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 = new char[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;            }        }        return new String(result, 0, len + resultOffset);    }

String. toLowerCase()

    public String toUpperCase() {        return toUpperCase(Locale.getDefault());    }    public String toUpperCase(Locale locale) {        // 类似与上面的toLowerCase        if (locale == null) {            throw new NullPointerException();        }        int firstLower;        final int len = value.length;        /* Now check if there are any characters that need to be changed. */        // 找到第一个小写字符        scan: {           for (firstLower = 0 ; firstLower < len; ) {                int c = (int)value[firstLower];                int srcCount;                if ((c >= Character.MIN_HIGH_SURROGATE)                        && (c <= Character.MAX_HIGH_SURROGATE)) {                    c = codePointAt(firstLower);                    srcCount = Character.charCount(c);                } else {                    srcCount = 1;                }                int upperCaseChar = Character.toUpperCaseEx(c);                if ((upperCaseChar == Character.ERROR)                        || (c != upperCaseChar)) {                    break scan;                }                firstLower += srcCount;            }            return this;        }        char[] result = new char[len]; /* may grow */        int resultOffset = 0;  /* result may grow, so i+resultOffset         * is the write location in result */        /* Just copy the first few upperCase characters. */        // 拷贝第一个小写字符前所有的大写字符        System.arraycopy(value, 0, result, 0, firstLower);        // ..        String lang = locale.getLanguage();        boolean localeDependent =                (lang == "tr" || lang == "az" || lang == "lt");        char[] upperCharArray;        int upperChar;        int srcChar;        int srcCount;        for (int i = firstLower; 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) {                upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);            } else {                upperChar = Character.toUpperCaseEx(srcChar);            }            if ((upperChar == Character.ERROR)                    || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {                if (upperChar == Character.ERROR) {                    if (localeDependent) {                        upperCharArray =                                ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);                    } else {                        upperCharArray = Character.toUpperCaseCharArray(srcChar);                    }                } else if (srcCount == 2) {                    resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;                    continue;                } else {                    upperCharArray = Character.toChars(upperChar);                }                /* Grow result if needed */                int mapLen = upperCharArray.length;                if (mapLen > srcCount) {                    char[] result2 = new char[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] = upperCharArray[x];                }                resultOffset += (mapLen - srcCount);            } else {                result[i + resultOffset] = (char)upperChar;            }        }        return new String(result, 0, len + resultOffset);    }

String. 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--;        }        // 如果行首和行尾都没有‘ ’ 返回this        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;    }

String. toString()

    public String toString() {        return this;    }

String. toCharArray()

public char[] toCharArray() {    // 为什么不能用Arrays.copyOf??        // Cannot use Arrays.copyOf because of class initialization order issues        // 新建一个字符数组 拷贝value的数据  并返回        char result[] = new char[value.length];        System.arraycopy(value, 0, result, 0, value.length);        return result;    }

String. format(String format, Object… args)

    public static String format(String format, Object... args) {        return new Formatter().format(format, args).toString();    }    public static String format(Locale l, String format, Object... args) {        return new Formatter(l).format(format, args).toString();    }

String. valueOf(Object obj)

public static String valueOf(Object obj) {    // 如果obj==null 返回”null”        return (obj == null) ? "null" : obj.toString();    }    public static String valueOf(char data[]) {        return new String(data);    }    public static String valueOf(char data[], int offset, int count) {        return new String(data, offset, count);    }

String. copyValueOf(char data[])

    public static String copyValueOf(char data[]) {        return new String(data);    }    public static String copyValueOf(char data[], int offset, int count) {        // All public String constructors now copy the data.        return new String(data, offset, count);    }

String. valueOf()

    public static String valueOf(boolean b) {        return b ? "true" : "false";    }    public static String valueOf(char c) {        char data[] = {c};        return new String(data, true);    }    public static String valueOf(int i) {        return Integer.toString(i);    }    public static String valueOf(long l) {        return Long.toString(l);    }    public static String valueOf(float f) {        return Float.toString(f);    }    public static String valueOf(double d) {        return Double.toString(d);}

String. intern()

    // 将该字符串拘留在字符串常量池    public native String intern();

String. HASHING_SEED

    /**     * Seed value used for each alternative hash calculated.     */    private static final int HASHING_SEED;    static {        long nanos = System.nanoTime();        long now = System.currentTimeMillis();        int SEED_MATERIAL[] = {                System.identityHashCode(String.class),                System.identityHashCode(System.class),                (int) (nanos >>> 32),                (int) nanos,                (int) (now >>> 32),                (int) now,                (int) (System.nanoTime() >>> 2)        };        // Use murmur3 to scramble the seeding material.        // Inline implementation to avoid loading classes        int h1 = 0;        // body        for (int k1 : SEED_MATERIAL) {            k1 *= 0xcc9e2d51;            k1 = (k1 << 15) | (k1 >>> 17);            k1 *= 0x1b873593;            h1 ^= k1;            h1 = (h1 << 13) | (h1 >>> 19);            h1 = h1 * 5 + 0xe6546b64;        }        // tail (always empty, as body is always 32-bit chunks)        // finalization        h1 ^= SEED_MATERIAL.length * 4;        // finalization mix force all bits of a hash block to avalanche        h1 ^= h1 >>> 16;        h1 *= 0x85ebca6b;        h1 ^= h1 >>> 13;        h1 *= 0xc2b2ae35;        h1 ^= h1 >>> 16;        HASHING_SEED = h1;    }    /**     * Cached value of the alternative hashing algorithm result     */private transient int hash32 = 0;

String. hash32()

    int hash32() {        int h = hash32;        if (0 == h) {           // harmless data race on hash32 here.           h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, 0, value.length);           // ensure result is not zero to avoid recalcing            // 确保hash32不为0  一面重复计算           h = (0 != h) ? h : 1;           hash32 = h;        }        return h;    }                                                                2015.01.15

->

ok, String 到此结束了

这个类是非常重要的, 建议详详细细的分析一遍 !


资源下载 : http://download.csdn.net/detail/u011039332/9047543

注 : 因为作者的水平有限,必然可能出现一些bug, 所以请大家指出!

0 0