{"id":38605,"date":"2024-11-26T09:41:23","date_gmt":"2024-11-26T01:41:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38605\/"},"modified":"2024-11-26T09:41:23","modified_gmt":"2024-11-26T01:41:23","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e5%85%a5%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38605\/","title":{"rendered":"java\u600e\u4e48\u628a\u5b57\u7b26\u4e32\u8f6c\u5165\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 tochararray() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f7f\u7528 stream api \u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u5e76\u6620\u5c04\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6309\u6a21\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 stringbuilder \u9010\u5b57\u7b26\u6784\u5efa\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111503454352428.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u5b57\u7b26\u4e32\u8f6c\u5165\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u5b57\u7b26\u4e32\u8f6c\u5165\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><br \/>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u6570\u7ec4\u662f\u6709\u5e8f\u5143\u7d20\u96c6\u5408\uff0c\u800c\u5b57\u7b26\u4e32\u662f\u5b57\u7b26\u5e8f\u5217\u3002<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 split() \u65b9\u6cd5<\/strong><br \/>*split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello World\";\nString[] arr = str.split(\" \"); \/\/ \u6309\u7a7a\u683c\u5206\u9694<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 2\uff1a\u4f7f\u7528 toCharArray() \u65b9\u6cd5<\/strong><br \/>*toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u3002\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String str = \"Hello World\";\nchar[] arr = str.toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 3\uff1a\u4f7f\u7528 Stream API<\/strong><br \/>*Stream API \u63d0\u4f9b\u4e86\u4e00\u4e2a\u66f4\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello World\";\nString[] arr = str.chars().mapToObj(c -&gt; String.valueOf((char) c)).toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 4\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><br \/>*\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello1World23\";\nString[] arr = str.split(\"[\\d]+\"); \/\/ \u6309\u6570\u5b57\u5206\u9694<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u65b9\u6cd5 5\uff1a\u4f7f\u7528 StringBuilder<\/strong><br \/>*StringBuilder \u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e2a\u53ef\u53d8\u5b57\u7b26\u5e8f\u5217\uff0c\u53ef\u4ee5\u9010\u4e2a\u5b57\u7b26\u5730\u6784\u5efa\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello World\";\nStringBuilder sb = new StringBuilder();\nfor (char c : str.toCharArray()) {\n  sb.append(c).append(\",\");\n}\nString[] arr = sb.toString().split(\",\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u548c\u6240\u9700\u8f93\u51fa\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u62c6\u5206\uff0csplit() \u65b9\u6cd5\u662f\u6700\u7b80\u5355\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u5b57\u7b26\u6570\u7ec4\uff0ctoCharArray() \u65b9\u6cd5\u66f4\u5408\u9002\u3002\u5f53\u9700\u8981\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002Stream API \u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u7b80\u6d01\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u800c StringBuilder \u9002\u7528\u4e8e\u9700\u8981\u6784\u5efa\u81ea\u5b9a\u4e49\u6570\u7ec4\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u5b57\u7b26\u4e32\u8f6c\u5165\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>\u5728 java \u4e2d\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\u6309\u6307\u5b9a\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 tochararray() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f7f\u7528 stream api \u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u5e76\u6620\u5c04\u4e3a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6309\u6a21\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 stringbuilder \u9010\u5b57\u7b26\u6784\u5efa\u6570\u7ec4\u3002 \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4 \u7b80\u4ecb\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u6570\u7ec4\u662f\u6709\u5e8f\u5143\u7d20\u96c6\u5408\uff0c\u800c\u5b57\u7b26\u4e32\u662f\u5b57\u7b26\u5e8f\u5217\u3002 \u65b9\u6cd5 1\uff1a\u4f7f\u7528 split() \u65b9\u6cd5*split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u4e3a\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a String str = &#8220;Hello World&#8221;; String[] arr = str.split(&#8221; &#8220;); \/\/ \u6309\u7a7a\u683c\u5206\u9694 \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 2\uff1a\u4f7f\u7528 toCharArray() \u65b9\u6cd5*toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\u3002\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String str = &#8220;Hello World&#8221;; char[] arr = str.toCharArray(); \u767b\u5f55\u540e\u590d\u5236 \u65b9\u6cd5 3\uff1a\u4f7f\u7528 Stream API*Stream API [&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-38605","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38605","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=38605"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38605\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}