{"id":38733,"date":"2024-11-26T13:00:14","date_gmt":"2024-11-26T05:00:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38733\/"},"modified":"2024-11-26T13:00:14","modified_gmt":"2024-11-26T05:00:14","slug":"java%e6%80%8e%e4%b9%88%e7%94%b1%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%9e%84%e9%80%a0%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38733\/","title":{"rendered":"java\u600e\u4e48\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u516d\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6784\u9020\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 string.split() \u65b9\u6cd5\u62c6\u5206\u5b57\u7b26\u4e32\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u4f7f\u7528\u5b57\u7b26\u6570\u7ec4\u548c arrays.copyofrange() \u65b9\u6cd5\u4f7f\u7528\u6d41\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b57\u7b26\u6d41\u4f7f\u7528 apache commons lang3 \u5e93\u4e2d\u7684 stringutils.split() \u65b9\u6cd5\u4f7f\u7528 guava \u5e93\u4e2d\u7684 splitter \u7c7b\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/15\/2024111505395174924.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u600e\u4e48\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Java \u4e2d\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4\u3002\u4e0b\u9762\u5217\u51fa\u4e86\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u5b57\u7b26\u4e32\u62c6\u5206<\/strong><\/p>\n<p>\u4f7f\u7528 String.split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String str = \"Java,Python,C++\";\nString[] languages = str.split(\",\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a<\/p>\n<pre>String str = \"Java-Python-C++\";\nString[] languages = str.split(\"-| \");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528\u5b57\u7b26\u6570\u7ec4<\/strong><\/p>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 Arrays.copyOfRange() \u65b9\u6cd5\u63d0\u53d6\u7279\u5b9a\u8303\u56f4\u7684\u5b57\u7b26\uff1a<\/p>\n<pre>String str = \"Java Python C++\";\nchar[] chars = str.toCharArray();\nchar[] languages = Arrays.copyOfRange(chars, 0, 12);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6d41<\/strong><\/p>\n<p>\u4f7f\u7528\u6d41\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b57\u7b26\u6d41\uff0c\u7136\u540e\u5c06\u5176\u6536\u96c6\u5230\u6570\u7ec4\u4e2d\uff1a<\/p>\n<pre>String str = \"Java Python C++\";\nString[] languages = str.chars()\n        .mapToObj(c -&gt; String.valueOf((char) c))\n        .toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u4f7f\u7528 Apache Commons Lang3 \u5e93<\/strong><\/p>\n<p>\u4f7f\u7528 Apache Commons Lang3 \u5e93\u4e2d\u7684 StringUtils.split() \u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u62c6\u5206\u9009\u9879\uff1a<\/p>\n<pre>String str = \"Java, Python, C++\";\nString[] languages = StringUtils.split(str, \",\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u4f7f\u7528 Guava \u5e93<\/strong><\/p>\n<p>\u4f7f\u7528 Guava \u5e93\u4e2d\u7684 Splitter \u7c7b\uff0c\u8be5\u7c7b\u63d0\u4f9b\u4e86\u7075\u6d3b\u4e14\u53ef\u81ea\u5b9a\u4e49\u7684\u62c6\u5206\u529f\u80fd\uff1a<\/p>\n<pre>String str = \"Java Python C++\";\nString[] languages = Splitter.on(' ').split(str).toArray();<\/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\u3001\u6240\u9700\u7684\u62c6\u5206\u7c92\u5ea6\u4ee5\u53ca\u53ef\u7528\u7684\u5e93\u3002\u5982\u679c\u60a8\u9700\u8981\u7cbe\u7ec6\u7684\u63a7\u5236\u6216\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\uff0c\u5219\u6b63\u5219\u8868\u8fbe\u5f0f\u6216 Apache Commons Lang3 \u5e93\u53ef\u80fd\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u7531\u5b57\u7b26\u4e32\u6784\u9020\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\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u516d\u79cd\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u6784\u9020\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 string.split() \u65b9\u6cd5\u62c6\u5206\u5b57\u7b26\u4e32\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u4f7f\u7528\u5b57\u7b26\u6570\u7ec4\u548c arrays.copyofrange() \u65b9\u6cd5\u4f7f\u7528\u6d41\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b57\u7b26\u6d41\u4f7f\u7528 apache commons lang3 \u5e93\u4e2d\u7684 stringutils.split() \u65b9\u6cd5\u4f7f\u7528 guava \u5e93\u4e2d\u7684 splitter \u7c7b \u5982\u4f55\u5728 Java \u4e2d\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u7531\u5b57\u7b26\u4e32\u6784\u9020\u6570\u7ec4\u3002\u4e0b\u9762\u5217\u51fa\u4e86\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u5b57\u7b26\u4e32\u62c6\u5206 \u4f7f\u7528 String.split() \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String str = &#8220;Java,Python,C++&#8221;; String[] languages = str.split(&#8220;,&#8221;); \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u4e3a\u5b50\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a String str = &#8220;Java-Python-C++&#8221;; String[] languages = str.split(&#8220;-| &#8220;); \u767b\u5f55\u540e\u590d\u5236 3. \u4f7f\u7528\u5b57\u7b26\u6570\u7ec4 \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u7b26\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528 Arrays.copyOfRange() [&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-38733","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38733","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=38733"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38733\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}