{"id":65945,"date":"2025-05-03T15:05:41","date_gmt":"2025-05-03T07:05:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65945\/"},"modified":"2025-05-03T15:05:41","modified_gmt":"2025-05-03T07:05:41","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8astring%e8%bd%ac%e6%88%90%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65945\/","title":{"rendered":"java\u600e\u4e48\u628astring\u8f6c\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5c06 string \u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u4f7f\u7528 string.split() \u65b9\u6cd5\uff0c\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32\u3002\u501f\u52a9 arrays.stream() \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8f6c\u6362\u4e3a\u6d41\u5e76\u6536\u96c6\u4e3a\u6570\u7ec4\u3002\u5229\u7528 java 8 \u7684 lambda \u8868\u8fbe\u5f0f\uff0c\u5b9e\u73b0\u66f4\u7b80\u6d01\u7684\u8f6c\u6362\u3002\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u5206\u9694\u7b26\uff0c\u901a\u8fc7\u5728\u5206\u9694\u7b26\u524d\u52a0\u4e0a \u6765\u5b9e\u73b0\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111316425019920.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628astring\u8f6c\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628astring\u8f6c\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 String \u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06 String \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 String.split() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u7ed9\u5b9a\u7684\u5206\u9694\u7b26\u5206\u5272\u4e3a\u4e00\u4e2a\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><strong>2. \u4f7f\u7528 Arrays.stream() \u65b9\u6cd5<\/strong><\/p>\n<p>\u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6d41\uff0c\u7136\u540e\u4f7f\u7528 Collectors.toList() \u65b9\u6cd5\u6536\u96c6\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello,World,Java\";\nString[] arr = str.chars().mapToObj(ch -&gt; (char) ch).toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Java 8 \u7684 lambda \u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5229\u7528 lambda \u8868\u8fbe\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7b80\u6d01\u7684\u8f6c\u6362\u65b9\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello,World,Java\";\nString[] arr = str.split(\",\").map(String::trim).toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u81ea\u5b9a\u4e49\u5206\u9694\u7b26<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728\u5206\u9694\u7b26\u524d\u9762\u52a0\u4e0a\u53cd\u659c\u6760 \u6765\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"Hello\nWorld\nJava\";\nString[] arr = str.split(\"\n\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u6ca1\u6709\u6307\u5b9a\u5206\u9694\u7b26\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u7a7a\u767d\u5b57\u7b26\uff08\u7a7a\u683c\u3001\u5236\u8868\u7b26\u3001\u6362\u884c\u7b26\u7b49\uff09\u3002<\/li>\n<li>\u7a7a\u5b57\u7b26\u4e32\u5c06\u5bfc\u81f4\u51fa\u73b0\u4e00\u4e2a\u957f\u5ea6\u4e3a 1 \u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628astring\u8f6c\u6210\u6570\u7ec4\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>\u5c06 string \u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u65b9\u6cd5\u6709\u56db\u79cd\uff1a\u4f7f\u7528 string.split() \u65b9\u6cd5\uff0c\u6839\u636e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32\u3002\u501f\u52a9 arrays.stream() \u65b9\u6cd5\uff0c\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u8f6c\u6362\u4e3a\u6d41\u5e76\u6536\u96c6\u4e3a\u6570\u7ec4\u3002\u5229\u7528 java 8 \u7684 lambda \u8868\u8fbe\u5f0f\uff0c\u5b9e\u73b0\u66f4\u7b80\u6d01\u7684\u8f6c\u6362\u3002\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u5206\u9694\u7b26\uff0c\u901a\u8fc7\u5728\u5206\u9694\u7b26\u524d\u52a0\u4e0a \u6765\u5b9e\u73b0\u3002 \u5982\u4f55\u5c06 String \u8f6c\u6362\u4e3a\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5c06 String \u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a 1. \u4f7f\u7528 String.split() \u65b9\u6cd5 \u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6309\u7167\u7ed9\u5b9a\u7684\u5206\u9694\u7b26\u5206\u5272\u4e3a\u4e00\u4e2a\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 2. \u4f7f\u7528 Arrays.stream() \u65b9\u6cd5 \u8be5\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6d41\uff0c\u7136\u540e\u4f7f\u7528 Collectors.toList() \u65b9\u6cd5\u6536\u96c6\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u4f8b\u5982\uff1a String str = &#8220;Hello,World,Java&#8221;; String[] arr = str.chars().mapToObj(ch -&gt; (char) ch).toArray(String[]::new); \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-65945","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65945","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=65945"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65945\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}