{"id":35495,"date":"2024-11-26T15:29:40","date_gmt":"2024-11-26T07:29:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/35495\/"},"modified":"2024-11-26T15:29:40","modified_gmt":"2024-11-26T07:29:40","slug":"java-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e8%bd%ac%e6%88%90%e6%95%b0%e7%bb%84%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/35495\/","title":{"rendered":"java \u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u6570\u7ec4\u5bf9\u8c61"},"content":{"rendered":"<blockquote><p>\n  \u5c06 java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u5bf9\u8c61\u7684\u65b9\u6cd5\u6709\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 arrays.stream() \u548c collectors.tolist() \u5c06\u5b57\u7b26\u4e32\u6d41\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/04\/2024110404183460250.jpg\" class=\"aligncenter\" title=\"java \u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" alt=\"java \u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u5bf9\u8c61<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u5bf9\u8c61\uff1a<\/p>\n<p><strong>1. split() \u65b9\u6cd5<\/strong><\/p>\n<p>split() \u65b9\u6cd5\u6839\u636e\u7ed9\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String str = \"Hello,world,java\";\nString[] arr = str.split(\",\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0carr \u6570\u7ec4\u5c06\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a<\/p>\n<p>[&#8220;Hello&#8221;, &#8220;world&#8221;, &#8220;java&#8221;]<\/p>\n<p><strong>2. toCharArray() \u65b9\u6cd5<\/strong><\/p>\n<p>toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello\";\nchar[] arr = str.toCharArray();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0carr \u6570\u7ec4\u5c06\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a<\/p>\n<p>[&#8216;H&#8217;, &#8216;e&#8217;, &#8216;l&#8217;, &#8216;l&#8217;, &#8216;o&#8217;]<\/p>\n<p><strong>3. Arrays.stream()<\/strong><\/p>\n<p>Arrays.stream() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6d41\uff0c\u7136\u540e\u4f7f\u7528 Collectors.toList() \u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import java.util.List;\nimport java.util.Arrays;\nimport java.util.stream.Collectors;\n\nString str = \"Hello,world,java\";\nList&lt;String&gt; arr = Arrays.stream(str.split(\",\")).collect(Collectors.toList());<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0carr \u5217\u8868\u5c06\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a<\/p>\n<p>[&#8220;Hello&#8221;, &#8220;world&#8221;, &#8220;java&#8221;]<\/p>\n<p><strong>4. RegEx<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>import java.util.regex.Pattern;\n\nString str = \"Hello,world,java\";\nString[] arr = Pattern.compile(\",\").split(str);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0carr \u6570\u7ec4\u5c06\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a<\/p>\n<p>[&#8220;Hello&#8221;, &#8220;world&#8221;, &#8220;java&#8221;]<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u5b57\u7b26\u4e32\u600e\u4e48\u8f6c\u6210\u6570\u7ec4\u5bf9\u8c61\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>\u5c06 java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u5bf9\u8c61\u7684\u65b9\u6cd5\u6709\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 arrays.stream() \u548c collectors.tolist() \u5c06\u5b57\u7b26\u4e32\u6d41\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u5206\u6210\u6570\u7ec4\u3002 \u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u5bf9\u8c61 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u5bf9\u8c61\uff1a 1. split() \u65b9\u6cd5 split() \u65b9\u6cd5\u6839\u636e\u7ed9\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u6570\u7ec4\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String str = &#8220;Hello,world,java&#8221;; String[] arr = str.split(&#8220;,&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0carr \u6570\u7ec4\u5c06\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a [&#8220;Hello&#8221;, &#8220;world&#8221;, &#8220;java&#8221;] 2. toCharArray() \u65b9\u6cd5 toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\u3002\u4f8b\u5982\uff1a String str = &#8220;Hello&#8221;; char[] arr = str.toCharArray(); \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c\u4ee5\u4e0a\u4ee3\u7801\u540e\uff0carr \u6570\u7ec4\u5c06\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a [&#8216;H&#8217;, &#8216;e&#8217;, [&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-35495","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35495","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=35495"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/35495\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=35495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=35495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=35495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}