
{"id":2136,"date":"2020-08-19T16:02:55","date_gmt":"2020-08-19T08:02:55","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2136"},"modified":"2020-08-23T08:17:55","modified_gmt":"2020-08-23T00:17:55","slug":"2020java%e6%95%99%e7%a8%8b%ef%bc%9asting%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2136","title":{"rendered":"2020java\u6559\u7a0b\uff1aSting\u7c7b"},"content":{"rendered":"<h3>Sting\u7c7b<\/h3>\n<p>\u5b57\u7b26\u4e32\u662f\u5e38\u91cf\uff0c\u521b\u5efa\u4e4b\u540e\u4e0d\u53ef\u6539\u53d8<br \/>\n\u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u6c60\u4e2d\uff0c\u53ef\u4ee5\u5171\u4eab<\/p>\n<pre>String s1=\"abc\";\r\nString s2=\"abc\";\r\nString s3=new String(\"abc\");\r\nSystem.out.println(s1 == s2);\/\/true\r\nSystem.out.println(s1 == s3);\/\/false<!--more-->\r\n\r\n<\/pre>\n<p>String s=&#8221;hello&#8221;;\/\/\u4ea7\u751f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b57\u7b26\u4e32\u6c60\u4e2d\u5b58\u50a8<br \/>\nString s=new String(&#8220;hello&#8221;);\/\/\u4ea7\u751f\u4e24\u4e2a\u5bf9\u8c61\uff0c\u5806\u3001\u6c60\u5404\u5b58\u50a8\u4e00\u4e2a\u3002<\/p>\n<p>\u5e38\u7528\u65b9\u6cd5<br \/>\npublic char charAt(int index);\/\/\u6839\u636e\u4e0b\u6807\u83b7\u53d6\u5b57\u7b26<\/p>\n<pre>String s=\"hello\";\r\nSystem.out.println(s.charAt(0));\/\/\u83b7\u53d6\u4e0b\u68070\u7684\u5b57\u7b26<\/pre>\n<pre>\/\/\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public char charAt(int index){\r\n        if ((index &lt; 0) || (index &gt;= value.length)) {\r\n            throw new StringIndexOutOfBoundsException(index);\r\n        }\r\n        return value[index];\r\n    }\r\n}<\/pre>\n<p>public boolean contains(String str);\/\/\u5224\u65ad\u5f53\u524d\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542bstr<\/p>\n<pre>String s=\"hello\";\r\nSystem.out.println(s.contains(\"lo\"));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public boolean contains(CharSequence s) {\r\n        return indexOf(s.toString()) &gt; -1;\r\n    }\r\n}*\/<\/pre>\n<p>pulic char[] toCharArray();\/\/\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u6570\u7ec4<\/p>\n<pre>String s=\"hello\";\r\nchar[] c=s.toCharArray();\r\nfor(int i=0;i&lt;c.length;i++){\r\n    System.out.print(c[i]);\r\n}<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public char[] toCharArray() {\r\n        \/\/ Cannot use Arrays.copyOf because of class initialization order issues\r\n        char result[] = new char[value.length];\r\n        System.arraycopy(value, 0, result, 0, value.length);\r\n        return result;\r\n    }\r\n}*\/<\/pre>\n<p>public int indexOf(String str);\/\/\u67e5\u627estr\u9996\u6b21\u51fa\u73b0\u7684\u4e0b\u6807\uff0c\u5b58\u5728\u8fd4\u56de\u4e0b\u6807\uff0c\u5426\u5219\u8fd4\u56de-1<\/p>\n<pre>String s=\"hello\";\r\nSystem.out.println(s.indexOf('e'));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public int indexOf(int ch) {\r\n        return indexOf(ch, 0);\r\n    }\r\n}*\/<\/pre>\n<p>public int lastIndexOf(String str);\/\/\u67e5\u627e\u5b57\u7b26\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u4e0b\u6807\u7d22\u5f15<\/p>\n<pre>String s=\"hello\";\r\nSystem.out.println(s.lastIndexOf('e'));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public int lastIndexOf(int ch) {\r\n        return lastIndexOf(ch, value.length - 1);\r\n    }\r\n}*\/<\/pre>\n<p>public int length();\/\/\u8fd4\u56de\u5b57\u7b26\u4e32\u957f\u5ea6<\/p>\n<pre>String s=\"hello\";\r\nSystem.out.println(s.length());<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public int length() {\r\n        return value.length;\r\n    }\r\n}*\/<\/pre>\n<p>public String trim();\/\/\u53bb\u6389\u5b57\u7b26\u4e32\u524d\u540e\u7684\u7a7a\u683c<\/p>\n<pre>String s=\" hello \";\r\nSystem.out.println(s.trim());<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public String trim() {\r\n        int len = value.length;\r\n        int st = 0;\r\n        char[] val = value;    \r\n\r\n        while ((st &lt; len) &amp;&amp; (val[st] &lt;= ' ')) {\r\n        st++;\r\n        }\r\n        while ((st &lt; len) &amp;&amp; (val[len - 1] &lt;= ' ')) {\r\n        len--;\r\n        }\r\n        return ((st &gt; 0) || (len &lt; value.length)) ? substring(st, len) : this;\r\n        }\r\n}*\/<\/pre>\n<p>public String toUpperCase();\/\/\u5c06\u5c0f\u5199\u8f6c\u6362\u6210\u5927\u5199<br \/>\npublic String toLowerCase();\/\/\u5c06\u5927\u5199\u8f6c\u6362\u6210\u5c0f\u5199<\/p>\n<pre>String s=\" hello \";\r\nSystem.out.println(s.toUpperCase());\r\nSystem.out.println(s.toLowerCase());<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\nprivate final char value[];\r\n    public String toUpperCase() {\r\n        return toUpperCase(Locale.getDefault());\r\n    }\r\n    public String toLowerCase() {\r\n        return toLowerCase(Locale.getDefault());\r\n    }\r\n}*\/<\/pre>\n<p>public boolean equalsIgnoreCase(String str);\/\/\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u4e0d\u8003\u8651\u5927\u5c0f\u5199<\/p>\n<pre>String s=\" hello \";\r\nString s1=s.toUpperCase();\r\nString s2=s.toLowerCase();\r\nSystem.out.println(s1.equals(s2));\r\nSystem.out.println(s1.equalsIgnoreCase(s2));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public boolean equalsIgnoreCase(String anotherString) {\r\n        return (this == anotherString) ? true\r\n                : (anotherString != null)\r\n                &amp;&amp; (anotherString.value.length == value.length)\r\n                &amp;&amp; regionMatches(true, 0, anotherString, 0, value.length);\r\n    }\r\n}*\/<\/pre>\n<p>public boolean endWith(String str);\/\/\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4ee5str\u7ed3\u5c3e<br \/>\npublic boolean startsWith(String str);\/\/\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4ee5str\u5f00\u5934<\/p>\n<pre>String s=\"hello.java\";\r\nSystem.out.println(s.endsWith(\".java\"));\r\nSystem.out.println(s.startsWith(\"hello\"));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public boolean endsWith(String suffix) {\r\n        return startsWith(suffix, value.length - suffix.value.length);\r\n    }\r\n    public boolean startsWith(String prefix) {\r\n        return startsWith(prefix, 0);\r\n    }\r\n}*\/<\/pre>\n<p>public String replace(char oldchar,char newchar);\/\/\u5c06\u65e7\u5b57\u7b26\u4e32\u8f6c\u6362\u6210\u65b0\u5b57\u7b26\u4e32<\/p>\n<pre>String s=\"hello\";\r\nSystem.out.println(s.replace('e','A'));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public String replace(char oldChar, char newChar) {\r\n        if (oldChar != newChar) {\r\n            int len = value.length;\r\n            int i = -1;\r\n            char[] val = value; \r\n\r\n            while (++i &lt; len) {\r\n        if (val[i] == oldChar) {\r\n        break;\r\n        }\r\n        }\r\n        if (i &lt; len) {\r\n        char buf[] = new char[len];\r\n        for (int j = 0; j &lt; i; j++) {\r\n        buf[j] = val[j];\r\n        }\r\n        while (i &lt; len) {\r\n        char c = val[i];\r\n        buf[i] = (c == oldChar) ? newChar : c;\r\n        i++;\r\n        }\r\n        return new String(buf, true);\r\n        }\r\n        }\r\n        return this;\r\n        }\r\n}*\/<\/pre>\n<p>public String[] split(String str);\/\/\u6839\u636estr\u62c6\u5206<\/p>\n<pre>String s=\"\u4e00\u4e8c\u4e09\u56db\u4e94\uff0c\u4e0a\u5c71\u6253\u8001\u864e\uff0c\u8001\u864e\u6ca1\u6253\u7740\uff0c\u6253\u5230\u4e00\u4e2a\u5c0f\u677e\u9f20\";\r\nString[] split = s.split(\"\uff0c\");\r\nfor(int i=0;i&lt;split.length;i++){\r\n    System.out.println(split[i]);\r\n}<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public String[] split(String regex) {\r\n        return split(regex, 0);\r\n    }\r\n}*\/<\/pre>\n<p>public String subString(int index);\/\/\u5b57\u7b26\u4e32\u622a\u53d6\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32<br \/>\npublic String subString(int startindex,int endindex);\/\/\u5b57\u7b26\u4e32\u622a\u53d6\uff0cindex\u4e3a\u5f00\u59cb\u622a\u53d6\u4f4d\u7f6e\uff0cendindex\u4e3a\u622a\u53d6\u6700\u540e\u7684\u4f4d\u7f6e-1<\/p>\n<pre>String s=\"HelloWorld\";\r\nSystem.out.println(s.substring(1));\r\nSystem.out.println(s.substring(2, 5));<\/pre>\n<pre>\/*\u6e90\u7801\r\npublic final class String implements java.io.Serializable,Comparable&lt;String&gt;,CharSequence{\r\n    private final char value[];\r\n    public String substring(int beginIndex) {\r\n        if (beginIndex &lt; 0) {\r\n            throw new StringIndexOutOfBoundsException(beginIndex);\r\n        }\r\n        int subLen = value.length - beginIndex;\r\n        if (subLen &lt; 0) {\r\n            throw new StringIndexOutOfBoundsException(subLen);\r\n        }\r\n        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);\r\n    }\r\n}*\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sting\u7c7b \u5b57\u7b26\u4e32\u662f\u5e38\u91cf\uff0c\u521b\u5efa\u4e4b\u540e\u4e0d\u53ef\u6539\u53d8 \u5b57\u7b26\u4e32\u5b57\u9762\u503c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u6c60\u4e2d\uff0c\u53ef\u4ee5\u5171\u4eab String s1=&#8221;a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2136","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2136"}],"version-history":[{"count":5,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2136\/revisions"}],"predecessor-version":[{"id":2146,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2136\/revisions\/2146"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}