{"id":264,"date":"2019-01-24T19:20:28","date_gmt":"2019-01-24T11:20:28","guid":{"rendered":"https:\/\/qtvz.com\/?p=264"},"modified":"2019-04-26T13:02:31","modified_gmt":"2019-04-26T05:02:31","slug":"jdk1-8-0_161_64%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e7%b3%bb%e5%88%97%e4%b9%8bjava-lang-string","status":"publish","type":"post","link":"https:\/\/qtvz.com\/264.html","title":{"rendered":"JDK1.8.0_161_64\u6e90\u7801\u89e3\u6790\u7cfb\u5217\u4e4bjava.lang.String"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression-->\n<div class=\"wp-block-file\"><a href=\"https:\/\/qtvz.com\/wp-content\/uploads\/2019\/01\/9a809661437d19f7.zip\">JAVA_JDK1.8.0_161_64\u6e90\u7801<\/a><a href=\"https:\/\/qtvz.com\/wp-content\/uploads\/2019\/01\/9a809661437d19f7.zip\" class=\"wp-block-file__button\" download>\u4e0b\u8f7d<\/a><\/div>\n\n\n\n<p class=\"has-background has-medium-font-size has-vivid-cyan-blue-background-color\">1\u3001\u63a5\u53e3\u89e3\u91ca<\/p>\n\n\n\n<p>\uff081\uff09Serializable \u8fd9\u4e2a\u5e8f\u5217\u5316\u63a5\u53e3\u6ca1\u6709\u4efb\u4f55\u65b9\u6cd5\u548c\u57df\uff0c\u4ec5\u7528\u4e8e\u6807\u8bc6\u5e8f\u5217\u5316\u7684\u8bed\u610f\u3002<\/p>\n\n\n\n<p>\uff082\uff09Comparable&lt;String&gt; \u7528\u4e8e\u5bf9\u4e24\u4e2a\u5b9e\u4f8b\u5316\u5bf9\u8c61\u6bd4\u8f83\u5927\u5c0f<\/p>\n\n\n\n<p>\uff083\uff09CharSequence \u8fd9\u4e2a\u63a5\u53e3\u662f\u4e00\u4e2a\u53ea\u8bfb\u7684\u5b57\u7b26\u5e8f\u5217\u3002\u5305\u62eclength(),&nbsp;<br>charAt(int index), subSequence(int start, int end)\u8fd9\u51e0\u4e2aAPI\u63a5\u53e3<\/p>\n\n\n\n<p class=\"has-background has-medium-font-size has-vivid-cyan-blue-background-color\">2\u3001\u4e3b\u8981\u53d8\u91cf<\/p>\n\n\n\n<p>\uff081\uff09private final char value[];<br>\u53ef\u4ee5\u770b\u5230\uff0cvalue[]\u662f\u5b58\u50a8String\u7684\u5185\u5bb9\u7684\uff0c\u5373\u5f53\u4f7f\u7528String str = \"abcd\";<br>\u7684\u65f6\u5019\uff0c\u672c\u8d28\u4e0a\uff0c\"abcd\"\u662f\u5b58\u50a8\u5728\u4e00\u4e2achar\u7c7b\u578b\u7684\u6570\u7ec4\u4e2d\u7684\u3002<\/p>\n\n\n\n<p>(2) private int hash;<\/p>\n\n\n\n<p>\u800chash\u662fString\u5b9e\u4f8b\u5316\u7684hashcode\u7684\u4e00\u4e2a\u7f13\u5b58\u3002\u56e0\u4e3aString\u7ecf\u5e38\u88ab\u7528\u4e8e\u6bd4\u8f83\uff0c\u6bd4\u5982\u5728HashMap\u4e2d\u3002<br>\u5982\u679c\u6bcf\u6b21\u8fdb\u884c\u6bd4\u8f83\u90fd\u91cd\u65b0\u8ba1\u7b97hashcode\u7684\u503c\u7684\u8bdd\uff0c\u90a3\u65e0\u7591\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\uff0c\u800c\u4fdd\u5b58\u4e00\u4e2ahashcode\u7684\u7f13\u5b58\u65e0\u7591\u80fd\u4f18\u5316\u8fd9\u6837\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\uff083\uff09private static final long serialVersionUID = -6849794470754667710L;<\/p>\n\n\n\n<p>Java\u7684\u5e8f\u5217\u5316\u673a\u5236\u662f\u901a\u8fc7\u5728\u8fd0\u884c\u65f6\u5224\u65ad\u7c7b\u7684serialVersionUID\u6765\u9a8c\u8bc1\u7248\u672c\u4e00\u81f4\u6027\u7684\u3002\u5728\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u65f6\uff0cJVM\u4f1a\u628a\u4f20\u6765<br>\u7684\u5b57\u8282\u6d41\u4e2d\u7684serialVersionUID\u4e0e\u672c\u5730\u76f8\u5e94\u5b9e\u4f53\uff08\u7c7b\uff09\u7684serialVersionUID\u8fdb\u884c\u6bd4\u8f83\uff0c\u5982\u679c\u76f8\u540c\u5c31\u8ba4\u4e3a\u662f\u4e00\u81f4\u7684\uff0c\u53ef\u4ee5\u8fdb\u884c\u53cd\u5e8f<br>\u5217\u5316\uff0c\u5426\u5219\u5c31\u4f1a\u51fa\u73b0\u5e8f\u5217\u5316\u7248\u672c\u4e0d\u4e00\u81f4\u7684\u5f02\u5e38\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u5e0c\u671b\u901a\u8fc7\u7f16\u8bd1\u6765\u5f3a\u5236\u5212\u5206\u8f6f\u4ef6\u7248\u672c\uff0c\u5373\u5b9e\u73b0\u5e8f\u5217\u5316\u63a5\u53e3\u7684\u5b9e\u4f53\u80fd\u591f\u517c\u5bb9\u5148\u524d\u7248\u672c\uff0c<br>\u672a\u4f5c\u66f4\u6539\u7684\u7c7b\uff0c\u5c31\u9700\u8981\u663e\u5f0f\u5730\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3aserialVersionUID\uff0c\u7c7b\u578b\u4e3along\u7684\u53d8\u91cf\uff0c\u4e0d\u4fee\u6539\u8fd9\u4e2a\u53d8\u91cf\u503c\u7684\u5e8f\u5217\u5316\u5b9e\u4f53\u90fd\u53ef\u4ee5\u76f8\u4e92\u8fdb\u884c<br>\u4e32\u884c\u5316\u548c\u53cd\u4e32\u884c\u5316<\/p>\n\n\n\n<p>\uff084\uff09 private static final ObjectStreamField[] serialPersistentFields =<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    new ObjectStreamField[0];<\/code><\/pre>\n\n\n\n<p class=\"has-background has-medium-font-size has-vivid-cyan-blue-background-color\">3\u3001\u6784\u9020\u65b9\u6cd5<\/p>\n\n\n\n<p> \uff081\uff09 public String()<br>\uff082\uff09 public String(String original)&nbsp;<br>\uff083\uff09 public String(char value[])<br>\uff084\uff09 public String(char value[], int offset, int count)<br>\uff085\uff09 public String(int[] codePoints, int offset, int count)<br>\uff086\uff09 public String(byte ascii[], int hibyte, int offset, int count)<br>\uff087\uff09 public String(byte ascii[], int hibyte)&nbsp;<br>\uff088\uff09 public String(byte bytes[], int offset, int length, String charsetName)<br>\uff089\uff09 public String(byte bytes[], int offset, int length, Charset charset)&nbsp;<br>\uff0810\uff09public String(byte bytes[], String charsetName)<br>\uff0811\uff09public String(byte bytes[], Charset charset)&nbsp;<br>\uff0812\uff09public String(byte bytes[], int offset, int length)<br>\uff0813\uff09public String(byte bytes[])&nbsp;<br>\uff0814\uff09public String(StringBuffer buffer)<br>\uff0815\uff09public String(StringBuilder builder)<br>String\u652f\u6301\u591a\u79cd\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u5305\u62ec\u63a5\u6536String\uff0cchar[],byte[],StringBuffer\u7b49\u591a\u79cd\u53c2\u6570\u7c7b\u578b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\u3002<br>\u4f46\u672c\u8d28\u4e0a\uff0c\u5176\u5b9e\u5c31\u662f\u5c06\u63a5\u6536\u5230\u7684\u53c2\u6570\u4f20\u9012\u7ed9\u5168\u5c40\u53d8\u91cfvalue[]\u3002 <\/p>\n\n\n\n<p>4\u3001\u5185\u90e8\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff081\uff09public int length() {   \n      return value.length;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff082\uff09public boolean isEmpty() { \n   return value.length == 0;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff083\uff09public char charAt(int index) {    \n      if ((index &lt; 0) || (index >= value.length)) {\n        throw new StringIndexOutOfBoundsException(index);\n    }\n    return value[index];\n  }<\/code><\/pre>\n\n\n\n<p>\u77e5\u9053\u4e86String\u5176\u5b9e\u5185\u90e8\u662f\u901a\u8fc7char[]\u5b9e\u73b0\u7684\uff0c\u90a3\u4e48\u5c31\u4e0d\u96be\u53d1\u73b0length()\uff0cisEmpty()\uff0ccharAt()\u8fd9\u4e9b\u65b9\u6cd5\u5176\u5b9e\u5c31\u662f\u5728\u5185\u90e8\u8c03\u7528\u6570\u7ec4\u7684\u65b9\u6cd5\u3002<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff084\uff09\/\/\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u7684\u4ee3\u7801\u70b9\npublic int codePointAt(int index) { \n  if ((index &lt; 0) || (index >= value.length)) {\n       throw new StringIndexOutOfBoundsException(index);\n   }\n   return Character.codePointAtImpl(value, index, value.length);<\/code><\/pre>\n\n\n\n<p>}<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u524d\u4e00\u4e2a\u4ee3\u7801\u70b9\n\uff085\uff09 public int codePointBefore(int index) {\n    int i = index - 1;\n    if ((i &lt; 0) || (i >= value.length)) {\n        throw new StringIndexOutOfBoundsException(index);\n    }\n    return Character.codePointBeforeImpl(value, index, 0);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\u8fd4\u56de\u6307\u5b9a\u8d77\u59cb\u5230\u7ed3\u675f\u6bb5\u5185\u5b57\u7b26\u4e2a\u6570\n\uff086\uff09public int codePointCount(int beginIndex, int endIndex) {\n    if (beginIndex &lt; 0 || endIndex > value.length || beginIndex > endIndex) {\n        throw new IndexOutOfBoundsException();\n    }\n    return Character.codePointCountImpl(value, beginIndex, endIndex - beginIndex);\n }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u3000\u3000\/\/\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u52a0\u4e0acodepointOffset\u540e\u5f97\u5230\u7684\u7d22\u5f15\u503c\n\uff087\uff09public int offsetByCodePoints(int index, int codePointOffset) {\n    if (index &lt; 0 || index > value.length) {\n        throw new IndexOutOfBoundsException();\n    }\n    return Character.offsetByCodePointsImpl(value, 0, value.length,\n            index, codePointOffset);\n}\n\n\/\/\u5c06\u5b57\u7b26\u4e32\u590d\u5236\u5230dst\u6570\u7ec4\u4e2d\uff0c\u590d\u5236\u5230dst\u6570\u7ec4\u4e2d\u7684\u8d77\u59cb\u4f4d\u7f6e\u53ef\u4ee5\u6307\u5b9a\u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8be5\u65b9\u6cd5\u5e76\u6ca1\u6709\u68c0\u6d4b\u590d\u5236\u5230dst\u6570\u7ec4\u540e\u662f\u5426\u8d8a\u754c\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff088\uff09 void getChars(char dst[], int dstBegin) {\n    System.arraycopy(value, 0, dst, dstBegin, value.length);\n}\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff089\uff09 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {\n    if (srcBegin &lt; 0) {\n        throw new StringIndexOutOfBoundsException(srcBegin);\n    }\n    if (srcEnd > value.length) {\n        throw new StringIndexOutOfBoundsException(srcEnd);\n    }\n    if (srcBegin > srcEnd) {\n        throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);\n    }\n    System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);\n}\n\/\/\u83b7\u53d6\u5f53\u524d\u5b57\u7b26\u4e32\u7684\u4e8c\u8fdb\u5236<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0810\uff09 public void getBytes(int srcBegin, int srcEnd, byte dst[], int dstBegin) {\n    if (srcBegin &lt; 0) {\n        throw new StringIndexOutOfBoundsException(srcBegin);\n    }\n    if (srcEnd > value.length) {\n        throw new StringIndexOutOfBoundsException(srcEnd);\n    }\n    if (srcBegin > srcEnd) {\n        throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);\n    }\n    Objects.requireNonNull(dst);\n\n    int j = dstBegin;\n    int n = srcEnd;\n    int i = srcBegin;\n    char[] val = value;   \/* avoid getfield opcode *\/\n\n    while (i &lt; n) {\n        dst[j++] = (byte)val[i++];\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0811\uff09public byte[] getBytes(String charsetName)\n        throws UnsupportedEncodingException {\n    if (charsetName == null) throw new NullPointerException();\n    return StringCoding.encode(charsetName, value, 0, value.length);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0812\uff09public byte[] getBytes() {\u3000\u3000\n     return\u00a0StringCoding.encode(value,\u00a00,\u00a0value.length);\u3000\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0813\uff09 public boolean equals(Object anObject) {  \n  if (this == anObject) {\n        return true;\n    }\n    if (anObject instanceof String) {\n        String anotherString = (String)anObject;\n        int n = value.length;\n        if (n == anotherString.value.length) {\n            char v1[] = value;\n            char v2[] = anotherString.value;\n            int i = 0;\n            while (n-- != 0) {\n                if (v1[i] != v2[i])\n                    return false;\n                i++;\n            }\n            return true;\n        }\n    }\n    return false;\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0814\uff09public int hashCode() {\n        int h = hash;\n        if (h == 0 &amp;&amp; value.length > 0) {\n            char val[] = value;\n\n            for (int i = 0; i &lt; value.length; i++) {\n                h = 31 * h + val[i];\n            }\n            hash = h;\n        }\n        return h;\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0815\uff09public boolean contentEquals(CharSequence cs) {\n    \/\/ Argument is a StringBuffer, StringBuilder\n    if (cs instanceof AbstractStringBuilder) {\n        if (cs instanceof StringBuffer) {\n            synchronized(cs) {\n               return nonSyncContentEquals((AbstractStringBuilder)cs);\n            }\n        } else {\n            return nonSyncContentEquals((AbstractStringBuilder)cs);\n        }\n    }\n    \/\/ Argument is a String\n    if (cs instanceof String) {\n        return equals(cs);\n    }\n    \/\/ Argument is a generic CharSequence\n    char v1[] = value;\n    int n = v1.length;\n    if (n != cs.length()) {\n        return false;\n    }\n    for (int i = 0; i &lt; n; i++) {\n        if (v1[i] != cs.charAt(i)) {\n            return false;\n        }\n    }\n    return true;\n   }\n\n   \u8fd9\u4e2a\u4e3b\u8981\u662f\u7528\u6765\u6bd4\u8f83String\u548cStringBuffer\u6216\u8005StringBuild\u7684\u5185\u5bb9\u662f\u5426\u4e00\u6837\u3002\u53ef\u4ee5\u770b\u5230\u4f20\u5165\u53c2\u6570\u662fCharSequence \uff0c\n   \u8fd9\u4e5f\u8bf4\u660e\u4e86StringBuffer\u548cStringBuild\u540c\u6837\u662f\u5b9e\u73b0\u4e86CharSequence\u3002\u6e90\u7801\u4e2d\u5148\u5224\u65ad\u53c2\u6570\u662f\u4ece\u54ea\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u5316\u6765\u7684\uff0c\n   \u518d\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u91c7\u7528\u4e0d\u540c\u7684\u65b9\u6848\uff0c\u4e0d\u8fc7\u5176\u5b9e\u5927\u4f53\u90fd\u662f\u91c7\u7528\u4e0a\u9762\u90a3\u4e2afor\u5faa\u73af\u7684\u65b9\u5f0f\u6765\u8fdb\u884c\u5224\u65ad\u4e24\u5b57\u7b26\u4e32\u662f\u5426\u5185\u5bb9\u76f8\u540c\u3002\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0816\uff09public int compareTo(String anotherString) {\n    int len1 = value.length;\n    int len2 = anotherString.value.length;\n    int lim = Math.min(len1, len2);\n    char v1[] = value;\n    char v2[] = anotherString.value;\n\n    int k = 0;\n    while (k &lt; lim) {\n        char c1 = v1[k];\n        char c2 = v2[k];\n        if (c1 != c2) {\n            return c1 - c2;\n        }\n        k++;\n    }\n    return len1 - len2;\n   }\n\n   \u8fd9\u4e2a\u5c31\u662fString\u5bf9Comparable\u63a5\u53e3\u4e2d\u65b9\u6cd5\u7684\u5b9e\u73b0\u4e86\u3002\u5176\u6838\u5fc3\u5c31\u662f\u90a3\u4e2awhile\u5faa\u73af\uff0c\u901a\u8fc7\u4ece\u7b2c\u4e00\u4e2a\u5f00\u59cb\u6bd4\u8f83\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\n   \u5f53\u9047\u5230\u7b2c\u4e00\u4e2a\u8f83\u5c0f\u7684\u5b57\u7b26\u65f6\uff0c\u5224\u5b9a\u8be5\u5b57\u7b26\u4e32\u5c0f\u3002\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0817\uff09public int compareTo(String anotherString) {\n    int len1 = value.length;\n    int len2 = anotherString.value.length;\n    int lim = Math.min(len1, len2);\n    char v1[] = value;\n    char v2[] = anotherString.value;\n\n    int k = 0;\n    while (k &lt; lim) {\n        char c1 = v1[k];\n        char c2 = v2[k];\n        if (c1 != c2) {\n            return c1 - c2;\n        }\n        k++;\n    }\n    return len1 - len2;\n}\n\u8fd9\u4e2a\u5c31\u662fString\u5bf9Comparable\u63a5\u53e3\u4e2d\u65b9\u6cd5\u7684\u5b9e\u73b0\u4e86\u3002\u5176\u6838\u5fc3\u5c31\u662f\u90a3\u4e2awhile\u5faa\u73af\uff0c\u901a\u8fc7\u4ece\u7b2c\u4e00\u4e2a\u5f00\u59cb\u6bd4\u8f83\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5f53\u9047\u5230\u7b2c\u4e00\u4e2a\u8f83\u5c0f\u7684\u5b57\u7b26\u65f6\uff0c\u5224\u5b9a\u8be5\u5b57\u7b26\u4e32\u5c0f\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> \uff0818\uff09public int compareToIgnoreCase(String str) {\n    return CASE_INSENSITIVE_ORDER.compare(this, str);\n}\n\u8fd9\u4e2a\u4e5f\u662f\u6bd4\u8f83\u5b57\u7b26\u4e32\u5927\u5c0f\uff0c\u89c4\u5219\u548c\u4e0a\u9762\u90a3\u4e2a\u6bd4\u8f83\u65b9\u6cd5\u57fa\u672c\u76f8\u540c\uff0c\u5dee\u522b\u5728\u4e8e\u8fd9\u4e2a\u65b9\u6cd5\u5ffd\u7565\u5927\u5c0f\u5199<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0819\uff09public boolean regionMatches(int toffset, String other, int ooffset,int len) {\n    char ta[] = value;\n    int to = toffset;\n    char pa[] = other.value;\n    int po = ooffset;\n    \/\/ Note: toffset, ooffset, or len might be near -1>>>1.\n    if ((ooffset &lt; 0) || (toffset &lt; 0)\n            || (toffset > (long)value.length - len)\n            || (ooffset > (long)other.value.length - len)) {\n        return false;\n    }\n    while (len-- > 0) {\n        if (ta[to++] != pa[po++]) {\n            return false;\n        }\n    }\n    return true;\n}\n\u6bd4\u8f83\u8be5\u5b57\u7b26\u4e32\u548c\u5176\u4ed6\u4e00\u4e2a\u5b57\u7b26\u4e32\u4ece\u5206\u522b\u6307\u5b9a\u5730\u70b9\u5f00\u59cb\u7684n\u4e2a\u5b57\u7b26\u662f\u5426\u76f8\u7b49\u3002\u770b\u4ee3\u7801\u53ef\u77e5\u9053\uff0c\u5176\u539f\u7406\u8fd8\u662f\u901a\u8fc7\u4e00\u4e2awhile\u53bb\u5faa\u73af\u5bf9\u5e94\u7684\u6bd4\u8f83\u533a\u57df\u8fdb\u884c\u5224\u65ad\uff0c\u4f46\u5728\u6bd4\u8f83\u4e4b\u524d\u4f1a\u505a\u5224\u5b9a\uff0c\u5224\u5b9a\u7ed9\u5b9a\u53c2\u6570\u662f\u5426\u8d8a\u754c\u3002\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0820\uff09 public boolean startsWith(String prefix, int toffset) {\n        char ta[] = value;\n        int to = toffset;\n        char pa[] = prefix.value;\n        int po = 0;\n        int pc = prefix.value.length;\n        \/\/ Note: toffset might be near -1>>>1.\n        if ((toffset &lt; 0) || (toffset > value.length - pc)) {\n            return false;\n        }\n        while (--pc >= 0) {\n            if (ta[to++] != pa[po++]) {\n                return false;\n            }\n        }\n        return true;\n    }  \n\u5224\u65ad\u5f53\u524d\u5b57\u7b26\u4e32\u662f\u5426\u4ee5\u67d0\u4e00\u6bb5\u5176\u4ed6\u5b57\u7b26\u4e32\u5f00\u59cb\u7684\uff0c\u548c\u5176\u4ed6\u5b57\u7b26\u4e32\u6bd4\u8f83\u65b9\u6cd5\u4e00\u6837\uff0c\u5176\u5b9e\u5c31\u662f\u901a\u8fc7\u4e00\u4e2awhile\u6765\u5faa\u73af\u6bd4\u8f83\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0821\uff09public int indexOf(int ch, int fromIndex) {\n        final int max = value.length;\n        if (fromIndex &lt; 0) {\n            fromIndex = 0;\n        } else if (fromIndex >= max) {\n            \/\/ Note: fromIndex might be near -1>>>1.\n            return -1;\n        }\n\n        if (ch &lt; Character.MIN_SUPPLEMENTARY_CODE_POINT) {\n            \/\/ handle most cases here (ch is a BMP code point or a\n            \/\/ negative value (invalid code point))\n            final char[] value = this.value;\n            for (int i = fromIndex; i &lt; max; i++) {\n                if (value[i] == ch) {\n                    return i;\n                }\n            }\n            return -1;\n        } else {\n            return indexOfSupplementary(ch, fromIndex);\n        }\n    }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0822\uff09 public int indexOf(int ch) {\n        return indexOf(ch, 0);\n    }\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0823\uff09 public String substring(int beginIndex) {\n        if (beginIndex &lt; 0) {\n            throw new StringIndexOutOfBoundsException(beginIndex);\n        }\n        int subLen = value.length - beginIndex;\n        if (subLen &lt; 0) {\n            throw new StringIndexOutOfBoundsException(subLen);\n        }\n        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);\n    }\n\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de\u5b57\u7b26\u4e32\u4e2d\u4e00\u4e2a\u5b50\u4e32\uff0c\u770b\u6700\u540e\u4e00\u884c\u53ef\u4ee5\u53d1\u73b0\uff0c\u5176\u5b9e\u5c31\u662f\u6307\u5b9a\u5934\u5c3e\uff0c\u7136\u540e\u6784\u9020\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0824\uff09 public String concat(String str) {\n        int otherLen = str.length();\n        if (otherLen == 0) {\n            return this;\n        }\n        int len = value.length;\n        char buf[] = Arrays.copyOf(value, len + otherLen);\n        str.getChars(buf, len);\n        return new String(buf, true);\n    }\n\n    concat\u7684\u4f5c\u7528\u662f\u5c06str\u62fc\u63a5\u5230\u5f53\u524d\u5b57\u7b26\u4e32\u540e\u9762\uff0c\u901a\u8fc7\u4ee3\u7801\u4e5f\u53ef\u4ee5\u770b\u51fa\u5176\u5b9e\u5c31\u662f\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0825\uff09 public String replace(char oldChar, char newChar) {\n        if (oldChar != newChar) {\n            int len = value.length;\n            int i = -1;\n            char[] val = value; \/* avoid getfield opcode *\/\n\n            while (++i &lt; len) {\n                if (val[i] == oldChar) {\n                    break;\n                }\n            }\n            if (i &lt; len) {\n                char buf[] = new char[len];\n                for (int j = 0; j &lt; i; j++) {\n                    buf[j] = val[j];\n                }\n                while (i &lt; len) {\n                    char c = val[i];\n                    buf[i] = (c == oldChar) ? newChar : c;\n                    i++;\n                }\n                return new String(buf, true);\n            }\n        }\n        return this;\n    }\n    \u66ff\u6362\u64cd\u4f5c\uff0c\u4e3b\u8981\u662f\u5c06\u539f\u6765\u5b57\u7b26\u4e32\u4e2d\u7684oldChar\u5168\u90e8\u66ff\u6362\u6210newChar\u3002\u770b\u8fd9\u91cc\u5b9e\u73b0\uff0c\u4e3b\u8981\u662f\u5148\u627e\u5230\u7b2c\u4e00\u4e2a\u6240\u8981\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e i \uff0c\n    \u5c06i\u4e4b\u524d\u7684\u5b57\u7b26\u76f4\u63a5\u590d\u5236\u5230\u4e00\u4e2a\u65b0char\u6570\u7ec4\u3002\u7136\u540e\u4ece i \u5f00\u59cb\u518d\u5bf9\u6bcf\u4e00\u4e2a\u5b57\u7b26\u8fdb\u884c\u5224\u65ad\u662f\u4e0d\u662f\u6240\u8981\u66ff\u6362\u7684\u5b57\u7b26\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0826\uff09 public String trim() {\n        int len = value.length;\n        int st = 0;\n        char[] val = value;    \/* avoid getfield opcode *\/\n\n        while ((st &lt; len) &amp;&amp; (val[st] &lt;= ' ')) {\n            st++;\n        }\n        while ((st &lt; len) &amp;&amp; (val[len - 1] &lt;= ' ')) {\n            len--;\n        }\n        return ((st > 0) || (len &lt; value.length)) ? substring(st, len) : this;\n    }\n\n    \u8fd9\u4e2a\u51fd\u6570\u5e73\u65f6\u7528\u7684\u5e94\u8be5\u6bd4\u8f83\u591a\uff0c\u5220\u9664\u5b57\u7b26\u4e32\u524d\u540e\u7684\u7a7a\u683c\uff0c\u539f\u7406\u662f\u901a\u8fc7\u627e\u51fa\u524d\u540e\u7b2c\u4e00\u4e2a\u4e0d\u662f\u7a7a\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u539f\u5b57\u7b26\u4e32\u7684\u8be5\u5b50\u4e32\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(27) public CharSequence subSequence(int beginIndex, int endIndex) {\n        return this.substring(beginIndex, endIndex);\n    }\n    \u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u7c7b\u578b\u7684\u5b57\u7b26\u4e32<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(28) public boolean startsWith(String prefix, int toffset) {\n      char ta[] = value;\n      int to = toffset;\n      char pa[] = prefix.value;\n      int po = 0;\n      int pc = prefix.value.length;\n      \/\/ Note: toffset might be near -1>>>1.\n      \/\/\u5982\u679c\u8d77\u59cb\u5730\u5740\u5c0f\u4e8e0\u6216\u8005\uff08\u8d77\u59cb\u5730\u5740+\u6240\u6bd4\u8f83\u5bf9\u8c61\u957f\u5ea6\uff09\u5927\u4e8e\u81ea\u8eab\u5bf9\u8c61\u957f\u5ea6\uff0c\u8fd4\u56de\u5047\n      if ((toffset &lt; 0) || (toffset > value.length - pc)) {\n          return false;\n      }\n      \/\/\u4ece\u6240\u6bd4\u8f83\u5bf9\u8c61\u7684\u672b\u5c3e\u5f00\u59cb\u6bd4\u8f83\n      while (--pc >= 0) {\n          if (ta[to++] != pa[po++]) {\n              return false;\n          }\n      }\n      return true;\n  }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(29) public boolean startsWith(String prefix) {\n      return startsWith(prefix, 0);\n  }\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(30) public boolean endsWith(String suffix) { \n     return startsWith(suffix, value.length - suffix.value.length);\n  }\n \u8d77\u59cb\u6bd4\u8f83\u548c\u672b\u5c3e\u6bd4\u8f83\u90fd\u662f\u6bd4\u8f83\u7ecf\u5e38\u7528\u5f97\u5230\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u5728\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u4e0d\u662fhttp\u534f\u8bae\u7684\uff0c\n \u6216\u8005\u521d\u6b65\u5224\u65ad\u4e00\u4e2a\u6587\u4ef6\u662f\u4e0d\u662fmp3\u6587\u4ef6\uff0c\u90fd\u53ef\u4ee5\u91c7\u7528\u8fd9\u4e2a\u65b9\u6cd5\u8fdb\u884c\u6bd4\u8f83\u3002<\/code><\/pre>\n\n\n\n<p>\n\n(31) public native String intern();\n\n<\/p>\n\n\n\n<p>intern\u65b9\u6cd5\u662fNative\u8c03\u7528\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u5728\u65b9\u6cd5\u533a\u4e2d\u7684\u5e38\u91cf\u6c60\u91cc\u901a\u8fc7equals\u65b9\u6cd5\u5bfb\u627e\u7b49\u503c\u7684\u5bf9\u8c61\uff0c<br>\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u5728\u5e38\u91cf\u6c60\u4e2d\u5f00\u8f9f\u4e00\u7247\u7a7a\u95f4\u5b58\u653e\u5b57\u7b26\u4e32\u5e76\u8fd4\u56de\u8be5\u5bf9\u5e94String\u7684\u5f15\u7528\uff0c<br>\u5426\u5219\u76f4\u63a5\u8fd4\u56de\u5e38\u91cf\u6c60\u4e2d\u5df2\u5b58\u5728String\u5bf9\u8c61\u7684\u5f15\u7528<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(32) public String toLowerCase() {\n    return toLowerCase(Locale.getDefault());\n }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(33) public String toUpperCase(Locale locale) {\n    if (locale == null) {\n        throw new NullPointerException();\n    }\n\n    int firstLower;\n    final int len = value.length;\n\n    \/* Now check if there are any characters that need to be changed. *\/\n    scan: {\n        for (firstLower = 0 ; firstLower &lt; len; ) {\n            int c = (int)value[firstLower];\n            int srcCount;\n            if ((c >= Character.MIN_HIGH_SURROGATE)\n                    &amp;&amp; (c &lt;= Character.MAX_HIGH_SURROGATE)) {\n                c = codePointAt(firstLower);\n                srcCount = Character.charCount(c);\n            } else {\n                srcCount = 1;\n            }\n            int upperCaseChar = Character.toUpperCaseEx(c);\n            if ((upperCaseChar == Character.ERROR)\n                    || (c != upperCaseChar)) {\n                break scan;\n            }\n            firstLower += srcCount;\n        }\n        return this;\n    }\n\n    \/* result may grow, so i+resultOffset is the write location in result *\/\n    int resultOffset = 0;\n    char[] result = new char[len]; \/* may grow *\/\n\n    \/* Just copy the first few upperCase characters. *\/\n    System.arraycopy(value, 0, result, 0, firstLower);\n\n    String lang = locale.getLanguage();\n    boolean localeDependent =\n            (lang == \"tr\" || lang == \"az\" || lang == \"lt\");\n    char[] upperCharArray;\n    int upperChar;\n    int srcChar;\n    int srcCount;\n    for (int i = firstLower; i &lt; len; i += srcCount) {\n        srcChar = (int)value[i];\n        if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &amp;&amp;\n            (char)srcChar &lt;= Character.MAX_HIGH_SURROGATE) {\n            srcChar = codePointAt(i);\n            srcCount = Character.charCount(srcChar);\n        } else {\n            srcCount = 1;\n        }\n        if (localeDependent) {\n            upperChar = ConditionalSpecialCasing.toUpperCaseEx(this, i, locale);\n        } else {\n            upperChar = Character.toUpperCaseEx(srcChar);\n        }\n        if ((upperChar == Character.ERROR)\n                || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {\n            if (upperChar == Character.ERROR) {\n                if (localeDependent) {\n                    upperCharArray =\n                            ConditionalSpecialCasing.toUpperCaseCharArray(this, i, locale);\n                } else {\n                    upperCharArray = Character.toUpperCaseCharArray(srcChar);\n                }\n            } else if (srcCount == 2) {\n                resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;\n                continue;\n            } else {\n                upperCharArray = Character.toChars(upperChar);\n            }\n\n            \/* Grow result if needed *\/\n            int mapLen = upperCharArray.length;\n            if (mapLen > srcCount) {\n                char[] result2 = new char[result.length + mapLen - srcCount];\n                System.arraycopy(result, 0, result2, 0, i + resultOffset);\n                result = result2;\n            }\n            for (int x = 0; x &lt; mapLen; ++x) {\n                result[i + resultOffset + x] = upperCharArray[x];\n            }\n            resultOffset += (mapLen - srcCount);\n        } else {\n            result[i + resultOffset] = (char)upperChar;\n        }\n    }\n    return new String(result, 0, len + resultOffset);\n}\n\u5c06\u5b57\u7b26\u4e32\u7684\u5927\u5199\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u5c06\u5b57\u7b26\u4e32\u5c0f\u5199\u8f6c\u6362\u4e3a\u5927\u5199<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0834\uff09public char[] toCharArray() {\n      char result[] = new char[value.length];\n      System.arraycopy(value, 0, result, 0, value.length);\n      return result;\n  }\n\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u6570\u7ec4\uff0c\u7531\u4e8e\u672c\u8eab\u5c31\u662f\u6570\u7ec4\u7684\u5f62\u5f0f\uff0c\u53ea\u9700\u5c06\u5176\u62f7\u8d1d\u5373\u53ef\u3002<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0835\uff09 public String[] split(String regex, int limit) {    \n    char ch = 0;\n    if (((regex.value.length == 1 &amp;&amp;\n         \".$|()[{^?*+\\\\\".indexOf(ch = regex.charAt(0)) == -1) ||\n         (regex.length() == 2 &amp;&amp;\n          regex.charAt(0) == '\\\\' &amp;&amp;\n          (((ch = regex.charAt(1))-'0')|('9'-ch)) &lt; 0 &amp;&amp;\n          ((ch-'a')|('z'-ch)) &lt; 0 &amp;&amp;\n          ((ch-'A')|('Z'-ch)) &lt; 0)) &amp;&amp;\n        (ch &lt; Character.MIN_HIGH_SURROGATE ||\n         ch > Character.MAX_LOW_SURROGATE))\n    {\n        int off = 0;\n        int next = 0;\n        boolean limited = limit > 0;\n        ArrayList&lt;String> list = new ArrayList&lt;>();\n        while ((next = indexOf(ch, off)) != -1) {\n            if (!limited || list.size() &lt; limit - 1) {\n                list.add(substring(off, next));\n                off = next + 1;\n            } else {    \/\/ last one\n                \/\/assert (list.size() == limit - 1);\n                list.add(substring(off, value.length));\n                off = value.length;\n                break;\n            }\n        }\n        \/\/ If no match was found, return this\n        if (off == 0)\n            return new String[]{this};\n\n        \/\/ Add remaining segment\n        if (!limited || list.size() &lt; limit)\n            list.add(substring(off, value.length));\n\n        \/\/ Construct result\n        int resultSize = list.size();\n        if (limit == 0) {\n            while (resultSize > 0 &amp;&amp; list.get(resultSize - 1).length() == 0) {\n                resultSize--;\n            }\n        }\n        String[] result = new String[resultSize];\n        return list.subList(0, resultSize).toArray(result);\n    }\n    return Pattern.compile(regex).split(this, limit);\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0836\uff09public String[] split(String regex) {\n    return split(regex, 0);\n }\n\n\u5bf9\u4e8e\u5b57\u7b26\u4e32 \"boo:and:foo\",regex\u4e3ao,limit\u4e3a5\u65f6\uff0c\nsplite\u65b9\u6cd5\u9996\u5148\u53bb\u5b57\u7b26\u4e32\u91cc\u67e5\u627eregex\u2014\u2014o\uff0c\u7136\u540e\u628ao\u505a\u4e3a\u5206\u9694\u7b26\uff0c\n\u9010\u4e2a\u628ao\u53bb\u6389\u5e76\u4e14\u628a\u5b57\u7b26\u4e32\u5206\u5f00\uff0c\u6bd4\u5982\uff0c\u53d1\u73b0b\u540e\u9762\u6709\u4e00\u4e2ao\uff0c\u4e8e\u662f\u628a\u8fd9\u4e2ao\u53bb\u6389\uff0c\n\u5e76\u4e14\u628a\u5b57\u7b26\u4e32\u62c6\u6210\"b\", \"o:and:foo\"\u4e24\u4e2a\u5b57\u7b26\u4e32\uff08\u6ce8\u610f\uff1ab\u540e\u9762\u7684\u4e24\u4e2ao\u5df2\u7ecf\u53bb\u6389\u4e86\u4e00\u4e2a\uff09\uff0c\n\u63a5\u4e0b\u6765\u770b\"o:and:foo\"\u8fd9\u4e2a\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e00\u4e2a\u5b57\u7b26\u5c31\u662fo\uff0c\n\u4e8e\u662fo\u524d\u9762\u76f8\u5f53\u4e8e\u4e00\u4e2a\u7a7a\u4e32\uff0c\u628a\u8fd9\u4e2ao\u53bb\u6389\uff0c\"o:and:foo\"\u88ab\u5206\u5f00\u6210\"\", \":and:foo\"\u8fd9\u6837\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\n\u4ee5\u6b64\u7c7b\u63a8\u5faa\u73af5\u6b21\u5c31\u662fsplite(\"o\", 5)\u65b9\u6cd5\u7684\u4f5c\u7528<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(37) public static String join(CharSequence delimiter, CharSequence... elements) {\n    Objects.requireNonNull(delimiter);\n    Objects.requireNonNull(elements);\n    \/\/ Number of elements not likely worth Arrays.stream overhead.\n    StringJoiner joiner = new StringJoiner(delimiter);\n    for (CharSequence cs: elements) {\n        joiner.add(cs);\n    }\n    return joiner.toString();\n }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>(38) public static String join(CharSequence delimiter,        Iterable&lt;? extends CharSequence> elements) {\n    Objects.requireNonNull(delimiter);\n    Objects.requireNonNull(elements);\n    StringJoiner joiner = new StringJoiner(delimiter);\n    for (CharSequence cs: elements) {\n        joiner.add(cs);\n    }\n    return joiner.toString();\n}\n\u5c06\u5df2\u7ed9\u7684\u6570\u7ec4\u7528\u7ed9\u5b9a\u7684\u5b57\u7b26\u8fdb\u884c\u5206\u5f00\uff0c\u8fd4\u56de\u4e00\u4e2a\u7279\u5b9a\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\nString [] strings = {\"a\",\"b\",\"c\",\"d\"};\n info =  String.join( \"a\",strings );\n System.out.println( info );\n\u6253\u5370\u7684\u7ed3\u679c\u4e3a aabacad<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0839\uff09 public static String format(String format, Object... args) {  \n      return new Formatter().format(format, args).toString();\n  }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\uff0840\uff09 public static String format(Locale l, String format, Object... args) { \n   return new Formatter(l).format(format, args).toString();\n  }\n  \u5c06\u5b57\u7b26\u4e32\u4e32\u683c\u5f0f\u5316\u4e3a\u4e00\u5b9a\u683c\u5f0f\u7684\u5f62\u5f0f\u8fd4\u56de\u7ed9<\/code><\/pre>\n\n\n\n<p>\uff0841\uff09 \u5404\u79cd\u5404\u6837\u7684 valueOf(Object obj) \u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f5c\u7528\u5c31\u662f\u5c06\u76f8\u5e94\u683c\u5f0f\u7684\u5bf9\u8c61\u8f6c\u6362\u4e3a\u4f60\u5b57\u7b26\u4e32\u7684\u683c\u5f0f<\/code><\/pre>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"1\u3001\u63a5\u53e3\u89e3\u91ca \uff081\uff09Serializable \u8fd9\u4e2a\u5e8f\u5217\u5316\u63a5\u53e3\u6ca1\u6709\u4efb\u4f55\u65b9\u6cd5\u548c\u57df\uff0c\u4ec5\u7528\u4e8e\u6807\u8bc6\u5e8f\u5217\u5316\u7684\u8bed\u610f\u3002 \uff082\uff09Comparable&lt;String&gt; \u7528\u4e8e\u5bf9\u4e24\u4e2a\u5b9e\u4f8b\u5316\u5bf9\u8c61\u6bd4\u8f83\u5927\u5c0f \uff083\uff09Ch \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[40],"tags":[69],"views":15892,"_links":{"self":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/264"}],"collection":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/comments?post=264"}],"version-history":[{"count":6,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":342,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/264\/revisions\/342"}],"wp:attachment":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/media?parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/categories?post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/tags?post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}