{"id":39852,"date":"2024-11-26T11:38:03","date_gmt":"2024-11-26T03:38:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/39852\/"},"modified":"2024-11-26T11:38:03","modified_gmt":"2024-11-26T03:38:03","slug":"java%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e6%94%b9%e6%88%90%e5%ad%97%e7%ac%a6%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/39852\/","title":{"rendered":"java\u5b57\u7b26\u4e32\u600e\u4e48\u6539\u6210\u5b57\u7b26\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5982\u4f55\u5c06 java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff1f\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528 charat() \u65b9\u6cd5\u4f7f\u7528 getchars() \u65b9\u6cd5\u4f7f\u7528 tochararray() \u65b9\u6cd5\uff08java 9+\uff09\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111711255657760.jpg\" class=\"aligncenter\" title=\"java\u5b57\u7b26\u4e32\u600e\u4e48\u6539\u6210\u5b57\u7b26\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u5b57\u7b26\u4e32\u600e\u4e48\u6539\u6210\u5b57\u7b26\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4<\/strong><\/p>\n<p>\u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 charAt() \u65b9\u6cd5<\/strong><\/p>\n<pre>String str = \"Hello World\";\nchar[] chars = new char[str.length()];\n\nfor (int i = 0; i &lt; str.length(); i++) {\n    chars[i] = str.charAt(i);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 getChars() \u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String str = \"Hello World\";\nchar[] chars = new char[str.length()];\nstr.getChars(0, str.length(), chars, 0);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 toCharArray() \u65b9\u6cd5\uff08Java 9+\uff09<\/strong><\/p>\n<p>Java 9 \u5f15\u5165\u4e86 toCharArray() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u76f4\u63a5\u5730\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff1a<\/p>\n<pre>String str = \"Hello World\";\nchar[] chars = str.toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u3002<\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u6570\u7ec4\u7684\u957f\u5ea6\u4e0e\u539f\u59cb\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u76f8\u540c\u3002<\/li>\n<li>\u5b57\u7b26\u6570\u7ec4\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u8fd9\u610f\u5473\u7740\u65e0\u6cd5\u4fee\u6539\u5176\u4e2d\u5305\u542b\u7684\u5b57\u7b26\u3002<\/li>\n<li>\u5b57\u7b26\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4ee5 Unicode \u7801\u70b9\u8868\u793a\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u5b57\u7b26\u4e32\u600e\u4e48\u6539\u6210\u5b57\u7b26\u6570\u7ec4\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5c06 java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff1f\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528 charat() \u65b9\u6cd5\u4f7f\u7528 getchars() \u65b9\u6cd5\u4f7f\u7528 tochararray() \u65b9\u6cd5\uff08java 9+\uff09 \u5982\u4f55\u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4 \u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a 1. \u4f7f\u7528 charAt() \u65b9\u6cd5 String str = &#8220;Hello World&#8221;; char[] chars = new char[str.length()]; for (int i = 0; i &lt; str.length(); i++) { chars[i] = str.charAt(i); } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 getChars() \u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String str = &#8220;Hello World&#8221;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-39852","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=39852"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/39852\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=39852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=39852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=39852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}