{"id":1096,"date":"2022-10-23T22:50:02","date_gmt":"2022-10-23T14:50:02","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1096"},"modified":"2022-11-27T12:39:50","modified_gmt":"2022-11-27T04:39:50","slug":"%e5%b8%b8%e7%94%a8%e7%b1%bb-%e5%9b%9e%e9%a1%bestring%e4%b8%8e%e5%9f%ba%e6%9c%ac%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e5%8c%85%e8%a3%85%e7%b1%bb%e7%9a%84%e8%bd%ac%e6%8d%a2%e3%80%81string%e4%b8%8echar","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1096","title":{"rendered":"\u5e38\u7528\u7c7b-\u56de\u987eString\u4e0e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u5305\u88c5\u7c7b\u7684\u8f6c\u6362\u3001String\u4e0echar[]\u4e4b\u95f4\u7684\u8f6c\u6362\u3001String\u4e0ebyte[]\u4e4b\u95f4\u7684\u8f6c\u6362"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P460;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.io.UnsupportedEncodingException;\r\nimport java.util.Arrays;\r\n\r\n\/**\r\n * \u6d89\u53ca\u5230String\u7c7b\u4e0e\u5176\u4ed6\u7ed3\u6784\u4e4b\u95f4\u7684\u8f6c\u6362\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-22 \u4e0b\u5348 5:34\r\n *\/\r\npublic class StringTest1 {\r\n\r\n    \/*\r\n    String \u4e0e byte&#91;]\u4e4b\u95f4\u7684\u8f6c\u6362\r\n    \u7f16\u7801\uff1aString --> byte&#91;]:\u8c03\u7528String\u7684getBytes()\r\n    \u89e3\u7801\uff1abyte&#91;] --> String:\u8c03\u7528String\u7684\u6784\u9020\u5668\r\n\r\n    \u7f16\u7801\uff1a\u5b57\u7b26\u4e32 --> \u5b57\u8282   (\u770b\u5f97\u61c2 ---> \u770b\u4e0d\u61c2\u7684\u4e8c\u8fdb\u5236\u6570\u636e)\r\n    \u89e3\u7801\uff1a\u7f16\u7801\u7684\u9006\u8fc7\u7a0b\uff0c\u5b57\u8282 --> \u5b57\u7b26\u4e32    (\u770b\u4e0d\u61c2\u7684\u4e8c\u8fdb\u5236\u6570\u636e ---> \u770b\u5f97\u61c2)\r\n\r\n    \u8bf4\u660e\uff1a\u89e3\u7801\u65f6\uff0c\u8981\u6c42\u89e3\u7801\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u5fc5\u987b\u4e0e\u7f16\u7801\u65f6\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\u4e00\u81f4\uff0c\u5426\u5219\u4f1a\u51fa\u73b0\u4e71\u7801\u3002\r\n     *\/\r\n\r\n    @Test\r\n    public void test3() throws UnsupportedEncodingException {\r\n        String str1 = \"abc123\u4e2d\u56fd\";\r\n        byte&#91;] bytes = str1.getBytes();\/\/\u4f7f\u7528\u9ed8\u8ba4\u7684\u5b57\u7b26\u96c6\uff0c\u8fdb\u884c\u7f16\u7801\u3002\r\n        System.out.println(Arrays.toString(bytes));\r\n\r\n        byte&#91;] gbks = str1.getBytes(\"gbk\");\/\/\u4f7f\u7528gbk\u5b57\u7b26\u96c6\u8fdb\u884c\u7f16\u7801\u3002\r\n        System.out.println(Arrays.toString(gbks));\r\n\r\n        System.out.println(\"**********************\");\r\n\r\n        String str2 = new String(bytes);\/\/\u4f7f\u7528\u9ed8\u8ba4\u7684\u5b57\u7b26\u96c6\uff0c\u8fdb\u884c\u89e3\u7801\u3002\r\n        System.out.println(str2);\r\n\r\n        String str3 = new String(gbks);\r\n        System.out.println(str3);\/\/\u51fa\u73b0\u4e71\u7801\u3002\u539f\u56e0\uff1a\u7f16\u7801\u96c6\u548c\u89e3\u7801\u96c6\u4e0d\u4e00\u81f4\r\n\r\n        String str4 = new String(gbks, \"gbk\");\r\n        System.out.println(str4);\/\/\u6ca1\u6709\u51fa\u73b0\u4e71\u7801\u3002\u539f\u56e0\uff1a\u7f16\u7801\u96c6\u548c\u89e3\u7801\u96c6\u4e00\u81f4\uff01\r\n\r\n\r\n    }\r\n\r\n    \/*\r\n    String \u4e0e char&#91;]\u4e4b\u95f4\u7684\u8f6c\u6362\r\n    String --> char&#91;]:\u8c03\u7528String\u7684toCharArray()\r\n    char&#91;] --> String:\u8c03\u7528String\u7684\u6784\u9020\u5668\r\n     *\/\r\n\r\n    @Test\r\n    public void test2(){\r\n        String str1 = \"abc123\";     \/\/\u9898\u76ee\uff1a a21cb3\r\n\r\n        char&#91;] charArray = str1.toCharArray();\r\n        for (int i = 0; i &lt; charArray.length; i++) {\r\n            System.out.println(charArray&#91;i]);\r\n\r\n        }\r\n\r\n        char&#91;] arr = new char&#91;]{'h','e','l','l','o'};\r\n        String str2 = new String(arr);\r\n        System.out.println(str2);\r\n    }\r\n\r\n\r\n    \/*\r\n    \u590d\u4e60\uff1a\r\n    String \u4e0e\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5305\u88c5\u7c7b\u4e4b\u95f4\u7684\u8f6c\u6362\u3002\r\n\r\n    String --> \u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5305\u88c5\u7c7b\uff1a\u8c03\u7528\u5305\u88c5\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\uff1aparseXxx(str)\r\n    \u57fa\u672c\u6570\u636e\u7c7b\u578b\u3001\u5305\u88c5\u7c7b --> String\uff1a\u8c03\u7528String\u91cd\u8f7d\u7684valueOf(xxx)\r\n     *\/\r\n\r\n    @Test\r\n    public void test1(){\r\n        String str1 = \"123\";\r\n\/\/        int num = (int)str1;\/\/\u9519\u8bef\u7684\r\n        int num = Integer.parseInt(str1);\r\n\r\n        String str2 = String.valueOf(num);\/\/\"123\"\r\n        String str3 = num + \"\";\r\n\r\n        System.out.println(str1 == str3);\r\n    }\r\n\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1096","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1096"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1096"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1096\/revisions"}],"predecessor-version":[{"id":1097,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1096\/revisions\/1097"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}