{"id":289,"date":"2019-01-29T16:45:25","date_gmt":"2019-01-29T08:45:25","guid":{"rendered":"https:\/\/qtvz.com\/?p=289"},"modified":"2019-01-29T16:45:26","modified_gmt":"2019-01-29T08:45:26","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-abstractstringbuilder","status":"publish","type":"post","link":"https:\/\/qtvz.com\/289.html","title":{"rendered":"JDK1.8.0_161_64\u6e90\u7801\u89e3\u6790\u7cfb\u5217\u4e4bjava.lang.AbstractStringBuilder"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression-->\n<h2>1\u3001\u7c7b\u7684\u58f0\u660e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract class AbstractStringBuilder implements Appendable, CharSequence<\/code><\/pre>\n\n\n\n<h4>1.1\u3001CharSequence<\/h4>\n\n\n\n<p>\u7528\u6765\u83b7\u53d6\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\u7684\u63a5\u53e3\uff1f<\/p>\n\n\n\n<p>1\u3001int length();<br> \u83b7\u53d6\u957f\u5ea6 \u539f\u6587\uff1a\uff08Returns the length of this character sequence\uff09<\/p>\n\n\n\n<p>2\u3001charAt(int index)<br> \u8fd4\u56de\u6307\u5b9a\u4f4d\u7f6e\u7684\u5b57\u7b26<\/p>\n\n\n\n<p>3\u3001subSequence(int start, int end);<br> \u8fd4\u56de\u4e00\u4e2a\u5e8f\u5217\u7684\u5b50\u5e8f\u5217\uff0c\u4f8b\u5982\uff0c\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u5b50\u4e32<\/p>\n\n\n\n<p>4\u3001toString() <br>\u8fd4\u56destring <\/p>\n\n\n\n<h4>1.2\u3001Appendable<\/h4>\n\n\n\n<p> \u5b9a\u4e49\u6dfb\u52a0\u89c4\u5219\uff0c\u4e5f\u5c31\u662f\u600e\u4e48\u5f80\u91cc\u9762\u6dfb\u52a0 <\/p>\n\n\n\n<h2>2\u3001\u7c7b\u7684\u53d8\u91cf<\/h2>\n\n\n\n<h4>2.1 char[] value;<\/h4>\n\n\n\n<p>\n\n\u7528\u6765\u5b58\u50a8\u7684\u5730\u65b9\n\n<\/p>\n\n\n\n<h4>2.2 int count<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u5b9e\u9645\u7528\u5230\u7684\u6570\u91cf<\/p><\/blockquote>\n\n\n\n<ul><li>\u662f\u53ef\u53d8\u7684<\/li><li>\u53ef\u76f4\u63a5\u5bf9value\u548ccount\u8fdb\u884c\u64cd\u4f5c<\/li><li>value \u662f\u52a8\u6001\u6570\u7ec4<\/li><li>value.length!=count;<\/li><li><\/li><\/ul>\n\n\n\n<h2>3\u3001\u6784\u9020\u51fd\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code> AbstractStringBuilder() {\n    }\n AbstractStringBuilder(int capacity) {\n        value = new char[capacity];\n    }<\/code><\/pre>\n\n\n\n<p> \u7b2c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u662f\u5e8f\u5217\u5316\u4e0d\u53ef\u7f3a\u5c11\u7684\uff0c\u7b2c\u4e8c\u4e2a\u662f\u7528\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u5bb9\u91cf\u7684AbstractStringBuilder <\/p>\n\n\n\n<h2>4\u3001\u57fa\u672c\u65b9\u6cd5<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code> public int length() {\n        return count;\n    }\n public int capacity() {\n        return value.length;\n    }\n<\/code><\/pre>\n\n\n\n<p> \u7b2c\u4e00\u4e2a\u8fd4\u56de\u5f53\u524d\u5b9e\u9645\u7528\u91cf\uff0c\u7b2c\u4e8c\u4e2a\u662f\u8fd4\u56de\u7684\u662f\u5bb9\u91cf\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u7b2c\u4e00\u4e2a\u8fd4\u56de\u7684\u662f\u6876\u91cc\u6c34\u7684\u591a\u5c11\uff0c\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u7684\u662f\u6876\u7684\u5927\u5c0f <\/p>\n\n\n\n<h2>5\u3001\u7a7a\u95f4\u5904\u7406<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code> public void ensureCapacity(int minimumCapacity) {\n\t        if (minimumCapacity > 0)\/\/\u5982\u679c>0\u5f80\u4e0b\u8d70\n\t            ensureCapacityInternal(minimumCapacity);\n\t    }\n \n\t   \n\t    private void ensureCapacityInternal(int minimumCapacity) {\n\t        \/\/ overflow-conscious code\n\t        if (minimumCapacity - value.length > 0) {\n\t        \t\/*\u5982\u679c\u9700\u8981\u7684\u6bd4\u73b0\u6709\u7684\u6700\u5927\u5bb9\u91cf\u8fd8\u8981\u5927\uff0c\u5219\u8fdb\u884c\u91cd\u65b0\u5206\u914dvalue*\/\n\t            value = Arrays.copyOf(value,\n\t                    newCapacity(minimumCapacity));\n\t        }\n\t    }\n \n \n\t   \/*\u5b9a\u4e49\u53ef\u5206\u914d\u7684\u6700\u5927\u503c*\/\n\t    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;\n \n\t    \n\t    private int newCapacity(int minCapacity) {\n\t        \/\/ overflow-conscious code\n\t        int newCapacity = (value.length &lt;&lt; 1) + 2;\/*\u6269\u5927\u4e24\u500d\u7a7a\u95f4*\/\n\t        if (newCapacity - minCapacity &lt; 0) {\/*\u5982\u679c\u73b0\u6709\u7684\u7a7a\u95f4\u6269\u5927\u4e24\u500d\u8fd8\u6ca1\u6709\u6240\u9700\u7684\u5927\uff0c\u5219\u628a\u6240\u9700\u7684\u4f5c\u4e3a\u65b0\u751f\u6210\u7684\u5bb9\u5668\u5927\u5c0f*\/\n\t            newCapacity = minCapacity;\n\t        }\n\t        return (newCapacity &lt;= 0 || MAX_ARRAY_SIZE - newCapacity &lt; 0)\n\t            ? hugeCapacity(minCapacity)\n\t            : newCapacity;\n\t    }\n \n\t    private int hugeCapacity(int minCapacity) {\n\t        if (Integer.MAX_VALUE - minCapacity &lt; 0) { \/\/ overflow\n\t            throw new OutOfMemoryError();\n\t        }\n\t        return (minCapacity > MAX_ARRAY_SIZE)\n\t            ? minCapacity : MAX_ARRAY_SIZE;\n\t    }\n<\/code><\/pre>\n\n\n\n<ul><li>\u5206\u914d\u7a7a\u95f4\u7684\u65f6\u5019\uff0c\u5148\u5224\u65ad\u6240\u9700\u8981\u7684\u7a7a\u95f4\u6570\u662f\u5426\u5927\u4e8e0<\/li><li>\u82e5\u5927\u4e8e0 \uff0c\u5219\u5224\u65ad\u662f\u5426\u5927\u4e8e\u73b0\u6709\u7684\u5bb9\u91cf<\/li><li>\u82e5\u5927\u4e8e\u73b0\u6709\u7684\u5bb9\u91cf\uff0c\u5219\u5bf9\u73b0\u6709\u7684\u5bb9\u91cf\u6269\u59272\u500d<\/li><li>\u82e5\u6269\u59272\u500d\u4ee5\u540e\u7684\u5bb9\u91cf\u8fd8\u6253\u4e0d\u5230\u6240\u9700\u7684\u5bb9\u91cf\uff0c\u5219\u76f4\u63a5\u6269\u5927\u5230\u6240\u9700\u7684\u5bb9\u91cf<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  public void trimToSize() {\n        if (count &lt; value.length) {\n            value = Arrays.copyOf(value, count);\n        }\n    }\n<\/code><\/pre>\n\n\n\n<ul><li>\u7528\u4e8e\u53bb\u9664\u6ca1\u7528\u7684\u7a7a\u95f4<\/li><li>\u8d39\u65f6\u95f4<\/li><\/ul>\n\n\n\n<ul><li>\u5982\u679c\u6876\u91cc\u7684\u6c34\u6bd4\u6876\u7684\u5bb9\u91cf\u5c0f\uff0c\u90a3\u4e48\u6362\u4e00\u4e2a\u548c\u6c34\u5bb9\u91cf\u4e00\u6837\u7684\u6876<\/li><\/ul>\n\n\n\n<h2>6\u3001append<\/h2>\n\n\n\n<h4 id=\"appendobject-obj\">append(Object obj)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder append(Object obj) {\n        return append(String.valueOf(obj));\n    }<\/code><\/pre>\n\n\n\n<p> \u8c03\u7528 append(String str) \u63a5\u53e3 <\/p>\n\n\n\n<h4 id=\"appendstring-str\">append(String str)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  public AbstractStringBuilder append(String str) {\n        if (str == null)\n            return appendNull();\n        int len = str.length();\n        ensureCapacityInternal(count + len);\n        str.getChars(0, len, value, count);\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p>null\u4e5f\u4f1a\u5728\u540e\u9762\u6dfb\u52a0<\/p>\n\n\n\n<p>\u5982\u679c\u4e3a\u7a7a\u6dfb\u52a0<br>\n1\u5904\u7684\u610f\u601d\u662f \u83b7\u53d6\u5b57\u7b26\u4e32\u957f\u5ea6<br>\n2\u5904\u7684\u610f\u601d\u662f \u5f00\u8f9f\u7a7a\u95f4\uff0c\u5f00\u8f9f\u5927\u5c0f\u4e3a\u539f\u5bb9\u91cf\u76842\u500d\u6216\u8005count+len\u7684\u5927\u5c0f<\/p>\n\n\n\n<p>3\u5904\u7684\u610f\u601d\u662f \u5c06str\u4e2d\u7684\u6570\u636e\u4ece 0 \u5f00\u59cb\uff0c\u5230 len \u7ed3\u675f \u590d\u5236\u5230value\u4e2d \u4ece count\u5f00\u59cb\u7684\u4f4d\u7f6e\uff0c\u901a\u4fd7\u7684\u8bf4\uff0c\u5c31\u662f\u628astr\u4e2d\u7684\u6570\u636e\u52a0\u8f7d\u5230value\u7684\u540e\u9762<\/p>\n\n\n\n<h4 id=\"appendstringbuffer-sb\">append(StringBuffer sb)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder append(StringBuffer sb) {\n        if (sb == null)\n            return appendNull();\n        int len = sb.length();\n        ensureCapacityInternal(count + len);\n        sb.getChars(0, len, value, count);\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p>\n\n\u4e0e \u4e0a\u4e00\u4e2a append(String str)\u7c7b\u4f3c\uff0c\u53ea\u662f\u91cd\u5199\u4e86\u590d\u5236\u65b9\u6cd5\uff0c\u590d\u5236\u53c2\u6570\u8fd8\u662f\u4e00\u6837\u7684\n\n<\/p>\n\n\n\n<h4 id=\"appendabstractstringbuilder-asb\">append(AbstractStringBuilder asb)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> AbstractStringBuilder append(AbstractStringBuilder asb) {\n        if (asb == null)\n            return appendNull();\n        int len = asb.length();\n        ensureCapacityInternal(count + len);\n        asb.getChars(0, len, value, count);\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p> \u76f8\u4f3c\u7684\u65b9\u6cd5 <\/p>\n\n\n\n<h4 id=\"appendcharsequence-s\">append(CharSequence s)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> @Override\n    public AbstractStringBuilder append(CharSequence s) {\n        if (s == null)\n            return appendNull();\n        if (s instanceof String)\n            return this.append((String)s);\n        if (s instanceof AbstractStringBuilder)\n            return this.append((AbstractStringBuilder)s);\n \n        return this.append(s, 0, s.length());\n    }<\/code><\/pre>\n\n\n\n<p>\n\n\u5bf9s\u8fdb\u884c\u5206\u7c7b\u5904\u7406\uff0c\u82e5\u4e0d\u5c5e\u4e8e\u4e0a\u9762\u7684\u7c7b\uff0c\u5219\u8c03\u7528\u672c\u5730\u65b9\u6cd5this.append(s, 0, s.length());\n\n<\/p>\n\n\n\n<h4 id=\"appendnull\">appendNull()<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> private AbstractStringBuilder appendNull() {\n        int c = count;\n        ensureCapacityInternal(c + 4);\n        final char[] value = this.value;\n        value[c++] = 'n';\n        value[c++] = 'u';\n        value[c++] = 'l';\n        value[c++] = 'l';\n        count = c;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p> \u770b\u6e90\u7801\u7684\u6837\u5b50\u662f\u628a \"null\" \u653e\u8fdb\u53bb\u4e86\u3002\u3002\u3002\u3002\u6655 \uff01<\/p>\n\n\n\n<h4 id=\"appendcharsequence-s-int-start-int-end\">append(CharSequence s, int start, int end)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder append(CharSequence s, int start, int end) {\n        if (s == null)\n            s = \"null\";\n        if ((start &lt; 0) || (start > end) || (end > s.length()))\n            throw new IndexOutOfBoundsException(\n                \"start \" + start + \", end \" + end + \", s.length() \"\n                + s.length());\n        int len = end - start;\n        ensureCapacityInternal(count + len);\n        for (int i = start, j = count; i &lt; end; i++, j++)\/\/1\n            value[j] = s.charAt(i);\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p>\u5176\u5b9e\u5c31\u662f\u901a\u8fc7charAt \u65b9\u6cd5\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\uff0c\u7136\u540e\u628a\u8fd9\u4e9b\u5b57\u7b26\u4e00\u4e2a\u4e2a\u52a0\u5230value\u540e\u9762<\/p>\n\n\n\n<ul><li>1 \u5904\u7684\u610f\u601d\u662f\u904d\u5386\u4e0bs\uff0c\u7136\u540e\u628as\u4e2d\u7684\u4e00\u4e2a\u4e2a\u90fd\u52a0\u5230value\u540e\uff08value\u5c31\u662f\u5b58\u50a8\u6570\u636e\u7684\u5730\u65b9\uff09<\/li><\/ul>\n\n\n\n<h4 id=\"appendchar-str\">append(char[] str)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder append(char[] str) {\n        int len = str.length;\n        ensureCapacityInternal(count + len);\n        System.arraycopy(str, 0, value, count, len);\/\/1\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p>\u76f4\u63a5\u8c03\u7528\u7684\u7cfb\u7edf\u7684\u590d\u5236\u65b9\u6cd5\uff0c\u5c06str\u590d\u5236\u5230value\u4e4b\u540e<\/p>\n\n\n\n<ul><li>1\u5904\u7684\u610f\u601d\u662f str\u5b57\u7b26\u4e32\uff0c 0 \u8981\u590d\u5236\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0cvalue \u76ee\u6807\u5b57\u7b26\u4e32\uff0ccount \u76ee\u6807\u5b57\u7b26\u4e32\u8d77\u59cb\u4f4d\u7f6e\uff0clen \u8981\u590d\u5236\u7684\u957f\u5ea6<\/li><\/ul>\n\n\n\n<h4 id=\"appendchar-str-int-offset-int-len\">append(char str[], int offset, int len)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder append(char str[], int offset, int len) {\n        if (len > 0)                \/\/ let arraycopy report AIOOBE for len &lt; 0\n            ensureCapacityInternal(count + len);\n        System.arraycopy(str, offset, value, count, len);\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p>\n\n\u76f8\u4f3c\u5904\u7406\u65b9\u6cd5\n\n<\/p>\n\n\n\n<h4 id=\"appendboolean-b\">append(boolean b)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder append(boolean b) {\n        if (b) {\n            ensureCapacityInternal(count + 4);\n            value[count++] = 't';\n            value[count++] = 'r';\n            value[count++] = 'u';\n            value[count++] = 'e';\n        } else {\n            ensureCapacityInternal(count + 5);\n            value[count++] = 'f';\n            value[count++] = 'a';\n            value[count++] = 'l';\n            value[count++] = 's';\n            value[count++] = 'e';\n        }\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p> \u5176\u5b9e\u5c31\u662f\u628a true\u548cfalse\u5b58\u5230 \u6570\u7ec4\u91cc\u9762<\/p>\n\n\n\n<h4>append(int i) <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  public AbstractStringBuilder append(int i) {\n        if (i == Integer.MIN_VALUE) {\n            append(\"-2147483648\");\n            return this;\n        }\n        int appendedLength = (i &lt; 0) ? Integer.stringSize(-i) + 1\n                                     : Integer.stringSize(i);\n        int spaceNeeded = count + appendedLength;\n        ensureCapacityInternal(spaceNeeded);\n        Integer.getChars(i, spaceNeeded, value);\n        count = spaceNeeded;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p> \u5904\u7406\u65b9\u6cd5\u76f8\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u590d\u5236\u65b9\u6cd5\u4e0d\u4e00\u6837 <\/p>\n\n\n\n<h2>7\u3001delete<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>  public AbstractStringBuilder delete(int start, int end) {\n        if (start &lt; 0)\n            throw new StringIndexOutOfBoundsException(start);\n        if (end > count)\n            end = count;\n        if (start > end)\n            throw new StringIndexOutOfBoundsException();\n        int len = end - start;\n        if (len > 0) {\n            System.arraycopy(value, start+len, value, start, count-end);\n            count -= len;\n        }\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p> \u5176\u5b9e\u5c31\u662f\u628a\u5927\u5bb9\u91cf\u6570\u636e\u590d\u5236\u5230\u5c0f\u5bb9\u91cf\u5bb9\u5668\u4e2d\uff0c\u591a\u4e8e\u7684\u90e8\u5206\u5c31\u88ab\u5220\u9664\u4e86 <\/p>\n\n\n\n<h2 id=\"7replace\">8\u3001replace<\/h2>\n\n\n\n<h4 id=\"replaceint-start-int-end-string-str\">replace(int start, int end, String str)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder replace(int start, int end, String str) {\n        if (start &lt; 0)\n            throw new StringIndexOutOfBoundsException(start);\n        if (start > count)\n            throw new StringIndexOutOfBoundsException(\"start > length()\");\n        if (start > end)\n            throw new StringIndexOutOfBoundsException(\"start > end\");\n \n        if (end > count)\n            end = count;\n        int len = str.length();\n        int newCount = count + len - (end - start);\/\/1\n        ensureCapacityInternal(newCount);\n \n        System.arraycopy(value, end, value, start + len, count - end);\/\/2\n        str.getChars(value, start);\/\/3\n        count = newCount;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p>1\u5904\u7684\u610f\u601d\u662f \u5c06\u73b0\u6709\u6570\u636e\u6bd4\u5143\u6570\u636e\u591a\u7684\u8d44\u6e90\u52a0\u4e0a<br>\n\u901a\u4fd7\u7406\u89e3\u5c31\u662f \u6709\u4e00\u4e2a\u88c5\u6ee1\u571f\u7684\u53e3\u888b\uff0c\u5fc3\u5728\u60f3\u628a\u91cc\u9762\u7684\u4e00\u90e8\u5206\u571f\u6362\u6210\u66f4\u5927\u7684\u77f3\u5934\uff0c\u4f46\u662f\u6362\u7684\u8bdd\uff0c\u7a7a\u95f4\u5c31\u4e0d\u591f\u4e86\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u628a\u5e03\u888b\u52a0\u5927\uff0c\u8fd9\u4e2a\u5927\u7684\u4f53\u79ef\u5c31\u662f \u77f3\u5934\u7684\u4f53\u79ef\u51cf\u53bb\u6362\u53bb\u7684\u6740\u7684\u4f53\u79ef<br>\n2\u5904\u7684\u610f\u601d\u662f \uff0c\u5c06\u4eceend\u4ee5\u540e\u7684\u6570\u636e\u6574\u4e2a\u632a\u5230start\u8fd9\u4e2a\u8282\u70b9\u540e<br>\n3\u5904\u7684\u610f\u601d\u662f\u5c06\u6570\u636e\u590d\u5236\u5230start\u5f00\u59cb\u4f4d\u7f6e<br>\n\u6574\u4e2a\u8fc7\u7a0b\u7684\u901a\u4fd7\u89e3\u91ca<br>\n1 \u589e\u52a0\u7a7a\u95f4<br>\n\u5148\u628a\u6e90\u5b57\u7b26\u4e32\u6bd4\u76ee\u6807\u591a\u7684\u7a7a\u95f4\u7533\u8bf7\u4e0a<br>\n2 \u817e\u4f4d\u7f6e<br>\n\u628a\u9700\u8981\u63d2\u5165\u7684\u6570\u636e\u4f4d\u7f6e\u817e\u51fa\u6765<br>\n3 \u5199\u6570\u636e<\/p>\n\n\n\n<p> \u5176\u5b9e \uff0c\u81ea\u5df1\u6839\u636e\u6e90\u7801\u753b\u4e2a\u56fe\u5c31\u51fa\u6765\u4e86 <\/p>\n\n\n\n<h2>9\u3001 substring <\/h2>\n\n\n\n<h4 id=\"substringint-start-int-end\">substring(int start, int end)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public String substring(int start, int end) {\n        if (start &lt; 0)\n            throw new StringIndexOutOfBoundsException(start);\n        if (end > count)\n            throw new StringIndexOutOfBoundsException(end);\n        if (start > end)\n            throw new StringIndexOutOfBoundsException(end - start);\n        return new String(value, start, end - start);\n    }<\/code><\/pre>\n\n\n\n<p>\n\n\u5c31\u662f\u65b0\u5efa\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\n\n<\/p>\n\n\n\n<h2>10\u3001insert<\/h2>\n\n\n\n<h4 id=\"insertint-index-char-str-int-offsetint-len\">insert(int index, char[] str, int offset,int len)<\/h4>\n\n\n\n<ul><li>index \u63d2\u5165\u7684\u4f4d\u7f6e<\/li><li>str \u63d2\u5165\u7684\u5185\u5bb9<\/li><li>offset \u63d2\u5165\u6570\u636e\u7684\u8d77\u59cb\u4f4d\u7f6e<\/li><li>len \u63d2\u5165\u6570\u636e\u7684\u957f\u5ea6<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>public AbstractStringBuilder insert(int index, char[] str, int offset, int len)\n    {\n        if ((index &lt; 0) || (index > length()))\n            throw new StringIndexOutOfBoundsException(index);\n        if ((offset &lt; 0) || (len &lt; 0) || (offset > str.length - len))\n            throw new StringIndexOutOfBoundsException(\n                \"offset \" + offset + \", len \" + len + \", str.length \"\n                + str.length);\n        ensureCapacityInternal(count + len);\/\/1\n        System.arraycopy(value, index, value, index + len, count - index);\/\/2\n        System.arraycopy(str, offset, value, index, len);\/\/3\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<ul><li>1\u5904\u7684\u610f\u601d\u662f\u5f00\u8f9f\u7a7a\u95f4<\/li><li>2\u5904\u7684\u610f\u601d\u662f\u632a\u4f4d\u7f6e<\/li><li>3\u5904\u7684\u610f\u601d\u662f\u63d2\u5165\u6570\u636e<\/li><\/ul>\n\n\n\n<h4 id=\"insertint-offset-string-str\">insert(int offset, String str)<\/h4>\n\n\n\n<ul><li>offset \u8981\u51fa\u5165\u7684\u4f4d\u7f6e<\/li><li>str \u8981\u63d2\u5165\u7684\u5b57\u7b26\u4e32<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder insert(int offset, String str) {\n        if ((offset &lt; 0) || (offset > length()))\n            throw new StringIndexOutOfBoundsException(offset);\n        if (str == null)\n            str = \"null\";\n        int len = str.length();\n        ensureCapacityInternal(count + len);\n        System.arraycopy(value, offset, value, offset + len, count - offset);\/\/1\n        str.getChars(value, offset);\/\/2\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<ul><li>1\u5904\u610f\u601d\u662f\u817e\u4f4d\u7f6e<\/li><li>2\u5904\u65f6\u63d2\u5165\u6570\u636e<\/li><\/ul>\n\n\n\n<h4 id=\"insertint-offset-char-str\">insert(int offset, char[] str)<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder insert(int offset, char[] str) {\n        if ((offset &lt; 0) || (offset > length()))\n            throw new StringIndexOutOfBoundsException(offset);\n        int len = str.length;\n        ensureCapacityInternal(count + len);\n        System.arraycopy(value, offset, value, offset + len, count - offset);\n        System.arraycopy(str, 0, value, offset, len);\n        count += len;\n        return this;\n    }<\/code><\/pre>\n\n\n\n<p> \u8ddf\u4e0a\u9762\u7684\u65b9\u6cd5\u5dee\u4e0d\u591a\u4e0d\u505a\u8be6\u89e3 <\/p>\n\n\n\n<h2 id=\"10reverse\">11\u3001reverse<\/h2>\n\n\n\n<p>\n\n\u5012\u88c5\n\n<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> public AbstractStringBuilder reverse() {\n        boolean hasSurrogates = false;\n        int n = count - 1;\n        for (int j = (n-1) >> 1; j >= 0; j--) {\n            int k = n - j;\n            char cj = value[j];\n            char ck = value[k];\n            value[j] = ck;\n            value[k] = cj;\n            if (Character.isSurrogate(cj) ||\n                Character.isSurrogate(ck)) {\n                hasSurrogates = true;\n            }\n        }\n        if (hasSurrogates) {\n            reverseAllValidSurrogatePairs();\n        }\n        return this;\n    }\n \n    \/** Outlined helper method for reverse() *\/\n    private void reverseAllValidSurrogatePairs() {\n        for (int i = 0; i &lt; count - 1; i++) {\n            char c2 = value[i];\n            if (Character.isLowSurrogate(c2)) {\n                char c1 = value[i + 1];\n                if (Character.isHighSurrogate(c1)) {\n                    value[i++] = c1;\n                    value[i] = c2;\n                }\n            }\n        }\n    }<\/code><\/pre>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"1\u3001\u7c7b\u7684\u58f0\u660e 1.1\u3001CharSequence \u7528\u6765\u83b7\u53d6\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\u7684\u63a5\u53e3\uff1f 1\u3001int length(); \u83b7\u53d6\u957f\u5ea6 \u539f\u6587\uff1a\uff08Returns the length of this character \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":13837,"_links":{"self":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/289"}],"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=289"}],"version-history":[{"count":2,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":291,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/posts\/289\/revisions\/291"}],"wp:attachment":[{"href":"https:\/\/qtvz.com\/api\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qtvz.com\/api\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}