{"id":65865,"date":"2025-05-03T09:11:23","date_gmt":"2025-05-03T01:11:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65865\/"},"modified":"2025-05-03T09:11:23","modified_gmt":"2025-05-03T01:11:23","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8achar%e6%95%b0%e7%bb%84%e8%bd%ac%e6%88%90string-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65865\/","title":{"rendered":"java\u600e\u4e48\u628achar\u6570\u7ec4\u8f6c\u6210string"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 string \u6784\u9020\u51fd\u6570\u3001stringjoiner \u7684 join() \u65b9\u6cd5\u548c arrays.tostring() \u65b9\u6cd5\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a string\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111313245631712.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628achar\u6570\u7ec4\u8f6c\u6210string\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628achar\u6570\u7ec4\u8f6c\u6210string\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a String<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a String\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 String \u6784\u9020\u51fd\u6570<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 String \u6784\u9020\u51fd\u6570\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String str = new String(charArray);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 join() \u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 StringJoiner \u7c7b\u7684 join() \u65b9\u6cd5\uff1a<\/p>\n<pre>String str = String.join(\"\", charArray);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Arrays.toString() \u65b9\u6cd5<\/strong><\/p>\n<p>Arrays \u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a toString() \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a String \u8868\u793a\u5f62\u5f0f\uff1a<\/p>\n<pre>String str = Arrays.toString(charArray);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u90fd\u4f1a\u751f\u6210\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684 String \u5bf9\u8c61\u3002<\/li>\n<li>\u4f7f\u7528 String \u6784\u9020\u51fd\u6570\u6216 StringJoiner.join() \u65b9\u6cd5\u65f6\uff0c\u5982\u679c char \u6570\u7ec4\u5305\u542b Unicode \u7f16\u7801\u5b57\u7b26\uff0c\u5219\u53ef\u80fd\u4f1a\u629b\u51fa IllegalArgumentException\u3002<\/li>\n<li>Arrays.toString() \u65b9\u6cd5\u5c06 char \u6570\u7ec4\u7684\u5143\u7d20\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u8868\u793a\u5f62\u5f0f\uff0c\u5e76\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u6765\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628achar\u6570\u7ec4\u8f6c\u6210string\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 string \u6784\u9020\u51fd\u6570\u3001stringjoiner \u7684 join() \u65b9\u6cd5\u548c arrays.tostring() \u65b9\u6cd5\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a string\u3002 \u5982\u4f55\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a String \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5c06 char \u6570\u7ec4\u8f6c\u6362\u4e3a String\uff1a 1. \u4f7f\u7528 String \u6784\u9020\u51fd\u6570 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 String \u6784\u9020\u51fd\u6570\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String str = new String(charArray); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 join() \u65b9\u6cd5 \u5728 Java 8 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 StringJoiner \u7c7b\u7684 join() \u65b9\u6cd5\uff1a String str = String.join(&#8220;&#8221;, charArray); \u767b\u5f55\u540e\u590d\u5236 [&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-65865","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65865","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=65865"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65865\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}