{"id":66968,"date":"2025-05-03T12:31:43","date_gmt":"2025-05-03T04:31:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/66968\/"},"modified":"2025-05-03T12:31:43","modified_gmt":"2025-05-03T04:31:43","slug":"java%e6%80%8e%e4%b9%88%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e5%8c%96%e6%88%90%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/66968\/","title":{"rendered":"java\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u6210\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7 split() \u65b9\u6cd5\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5305\u542b\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u53ef\u4ee5\u901a\u8fc7\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6765\u6307\u5b9a\u3002\u6b64\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u4f8b\u5982 character.codepoints()\u3001string.tochararray() \u548c stringutils.split()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/17\/2024111721064335565.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u6210\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\u6210\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p><strong>\u76f4\u63a5\u7b54\u6848\uff1a<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4ee5\u7279\u5b9a\u7684\u5206\u9694\u7b26\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><strong>\u8be6\u7ec6\u8bf4\u660e\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>split() \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u4f5c\u4e3a\u5206\u9694\u7b26\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u5c06\u5b57\u7b26\u4e32\u5728\u6b64\u5206\u9694\u7b26\u5904\u5206\u5272\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"1,2,3,4,5\";\nString[] arr = str.split(\",\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>arr \u73b0\u5728\u5305\u542b\u4e86\u4ee5\u4e0b\u5143\u7d20\uff1a<\/p>\n<pre>[\"1\", \"2\", \"3\", \"4\", \"5\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\uff1a<\/strong><\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csplit() \u65b9\u6cd5\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6765\u6307\u5b9a\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>String str = \"1,2,3,4,5\";\nString[] arr = str.split(\",\");\nString[] arr2 = str.split(\" \", 2); \/\/ \u4ee5\u7a7a\u683c\u5206\u9694\uff0c\u6700\u591a 2 \u6b21<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>arr2 \u73b0\u5728\u5305\u542b\u4ee5\u4e0b\u5143\u7d20\uff1a<\/p>\n<pre>[\"1,2\", \"3,4,5\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u9664\u4e86 split() \u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 Character.codePoints() \u65b9\u6cd5\u5c06 Unicode \u5b57\u7b26\u70b9\u8f6c\u6362\u4e3a int \u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528 String.toCharArray() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5b57\u7b26\u8f6c\u6362\u4e3a char \u6570\u7ec4\u3002<\/li>\n<li>\u4f7f\u7528 Apache Commons Lang3 \u4e2d\u7684 StringUtils.split() \u65b9\u6cd5\uff08\u4ec5\u9002\u7528\u4e8e\u539f\u59cb\u503c\uff0c\u4e0d\u53ef\u53d8\u5b57\u7b26\u4e32\uff09\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u5c06\u5b57\u7b26\u4e32\u8f6c\u5316\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>\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7 split() \u65b9\u6cd5\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u8be5\u65b9\u6cd5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5305\u542b\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u53ef\u4ee5\u901a\u8fc7\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6765\u6307\u5b9a\u3002\u6b64\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u4f8b\u5982 character.codepoints()\u3001string.tochararray() \u548c stringutils.split()\u3002 \u5982\u4f55\u5c06 Java \u4e2d\u7684\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4 \u76f4\u63a5\u7b54\u6848\uff1a \u53ef\u4ee5\u4f7f\u7528 split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u4ee5\u7279\u5b9a\u7684\u5206\u9694\u7b26\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002 \u8be6\u7ec6\u8bf4\u660e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b split() \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a\u4f5c\u4e3a\u5206\u9694\u7b26\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5e76\u5c06\u5b57\u7b26\u4e32\u5728\u6b64\u5206\u9694\u7b26\u5904\u5206\u5272\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u7ec4\u3002\u4f8b\u5982\uff1a String str = &#8220;1,2,3,4,5&#8221;; String[] arr = str.split(&#8220;,&#8221;); \u767b\u5f55\u540e\u590d\u5236 arr \u73b0\u5728\u5305\u542b\u4e86\u4ee5\u4e0b\u5143\u7d20\uff1a [&#8220;1&#8221;, &#8220;2&#8221;, &#8220;3&#8221;, &#8220;4&#8221;, &#8220;5&#8221;] \u767b\u5f55\u540e\u590d\u5236 \u81ea\u5b9a\u4e49\u5206\u9694\u7b26\uff1a \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0csplit() \u65b9\u6cd5\u4f7f\u7528\u7a7a\u683c\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u4f46\u662f\uff0c\u53ef\u4ee5\u901a\u8fc7\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u6765\u6307\u5b9a\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\u3002\u4f8b\u5982\uff1a String str = &#8220;1,2,3,4,5&#8221;; String[] arr = str.split(&#8220;,&#8221;); String[] arr2 = str.split(&#8221; &#8220;, 2); \/\/ \u4ee5\u7a7a\u683c\u5206\u9694\uff0c\u6700\u591a 2 \u6b21 [&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-66968","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66968","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=66968"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/66968\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=66968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=66968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=66968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}