{"id":40056,"date":"2024-11-26T17:20:28","date_gmt":"2024-11-26T09:20:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40056\/"},"modified":"2024-11-26T17:20:28","modified_gmt":"2024-11-26T09:20:28","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e4%b8%80%e4%b8%b2%e5%ad%97%e7%ac%a6%e5%8f%98%e6%88%90%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40056\/","title":{"rendered":"java\u600e\u4e48\u628a\u4e00\u4e32\u5b57\u7b26\u53d8\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\u6839\u636e\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 tochararray() \u65b9\u6cd5\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f7f\u7528 stream api \u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u6620\u5c04\u5230\u4e00\u4e2a character \u5bf9\u8c61\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6839\u636e\u590d\u6742\u6a21\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111719243863937.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u4e00\u4e32\u5b57\u7b26\u53d8\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u4e00\u4e32\u5b57\u7b26\u53d8\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u800c\u6570\u7ec4\u662f\u5143\u7d20\u7684\u96c6\u5408\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 split() \u65b9\u6cd5<\/strong><\/p>\n<p>split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u5b50\u4e32\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5206\u9694\u7b26\uff08\u5982\u7a7a\u683c\u3001\u9017\u53f7\u6216\u5236\u8868\u7b26\uff09\u6765\u6307\u5b9a\u5982\u4f55\u62c6\u5206\u5b57\u7b26\u4e32\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String str = \"My name is John Doe\";\nString[] arr = str.split(\" \"); \/\/ \u6839\u636e\u7a7a\u683c\u62c6\u5206<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 toCharArray() \u65b9\u6cd5<\/strong><\/p>\n<p>toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u3002<\/p>\n<pre>String str = \"My name is John Doe\";\nchar[] arr = str.toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Stream API<\/strong><\/p>\n<p>Stream API \u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u65b9\u6cd5\u6765\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 mapToObj() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u6620\u5c04\u5230\u4e00\u4e2a Character \u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5176\u6536\u96c6\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002<\/p>\n<pre>import java.util.stream.Collectors;\n\nString str = \"My name is John Doe\";\nCharacter[] arr = str.chars()\n        .mapToObj(c -&gt; (char) c)\n        .toArray(Character[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u6839\u636e\u590d\u6742\u7684\u6a21\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002Pattern.split() \u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u6b63\u5219\u8868\u8fbe\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>import java.util.regex.Pattern;\n\nString str = \"My name is John Doe\";\nString[] arr = Pattern.compile(\"\\s+\").split(str); \/\/ \u6839\u636e\u591a\u4e2a\u7a7a\u683c\u62c6\u5206<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u4e00\u4e32\u5b57\u7b26\u53d8\u6210\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>\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\u6839\u636e\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 tochararray() \u65b9\u6cd5\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f7f\u7528 stream api \u5c06\u5b57\u7b26\u4e32\u7684\u6bcf\u4e2a\u5b57\u7b26\u6620\u5c04\u5230\u4e00\u4e2a character \u5bf9\u8c61\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6839\u636e\u590d\u6742\u6a21\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002 \u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u53d8\u7684\u5b57\u7b26\u5e8f\u5217\uff0c\u800c\u6570\u7ec4\u662f\u5143\u7d20\u7684\u96c6\u5408\u3002\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 split() \u65b9\u6cd5 split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u5b50\u4e32\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5206\u9694\u7b26\uff08\u5982\u7a7a\u683c\u3001\u9017\u53f7\u6216\u5236\u8868\u7b26\uff09\u6765\u6307\u5b9a\u5982\u4f55\u62c6\u5206\u5b57\u7b26\u4e32\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String str = &#8220;My name is John Doe&#8221;; String[] arr = str.split(&#8221; &#8220;); \/\/ \u6839\u636e\u7a7a\u683c\u62c6\u5206 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 toCharArray() \u65b9\u6cd5 toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5b57\u7b26\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u5b57\u7b26\u4e32\u4e2d\u7684\u4e00\u4e2a\u5b57\u7b26\u3002 String str = &#8220;My name is John Doe&#8221;; char[] arr = str.toCharArray(); \u767b\u5f55\u540e\u590d\u5236 3. [&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-40056","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40056","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=40056"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40056\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}