{"id":67043,"date":"2025-05-03T10:07:01","date_gmt":"2025-05-03T02:07:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/67043\/"},"modified":"2025-05-03T10:07:01","modified_gmt":"2025-05-03T02:07:01","slug":"java-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%80%8e%e4%b9%88%e5%90%88%e5%b9%b6%e6%88%90%e4%b8%80%e4%b8%aa%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/67043\/","title":{"rendered":"java \u5b57\u7b26\u4e32\u600e\u4e48\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\uff0c\u6709\u56db\u79cd\u65b9\u6cd5\u53ef\u5c06\u5b57\u7b26\u4e32\u5408\u5e76\u6210\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\uff1a\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 stringtokenizer\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 scanner\uff1a\u9010\u4e2a\u89e3\u6790\u5b57\u7b26\u4e32\u5355\u8bcd\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u5339\u914d\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u5e76\u521b\u5efa\u6570\u7ec4\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u5efa\u8bae\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/18\/2024111816130082436.jpg\" class=\"aligncenter\" title=\"java \u5b57\u7b26\u4e32\u600e\u4e48\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4\u63d2\u56fe\" alt=\"java \u5b57\u7b26\u4e32\u600e\u4e48\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u5b57\u7b26\u4e32\u5408\u5e76\u6210\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 split() \u65b9\u6cd5<\/strong><\/p>\n<p>split() \u65b9\u6cd5\u53ef\u57fa\u4e8e\u6307\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62c6\u5206\uff0c\u5f62\u6210\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] words = \"Hello World\".split(\" \"); \/\/ [\"Hello\", \"World\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 StringTokenizer<\/strong><\/p>\n<p>StringTokenizer \u7c7b\u53ef\u57fa\u4e8e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u4ee4\u724c\uff08tokens\uff09\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u679a\u4e3e\u6765\u904d\u5386\u8fd9\u4e9b\u4ee4\u724c\u3002\u53ef\u4ee5\u4f7f\u7528 toArray() \u65b9\u6cd5\u5c06\u4ee4\u724c\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>StringTokenizer tokenizer = new StringTokenizer(\"Hello World\", \" \");\nString[] words = tokenizer.toArray(new String[0]); \/\/ [\"Hello\", \"World\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u4f7f\u7528 Scanner<\/strong><\/p>\n<p>Scanner \u7c7b\u53ef\u7528\u4e8e\u4ece\u5b57\u7b26\u4e32\u4e2d\u626b\u63cf\u6570\u636e\u3002\u4f7f\u7528 hasNext() \u548c next() \u65b9\u6cd5\u53ef\u4ee5\u9010\u4e2a\u89e3\u6790\u5355\u8bcd\uff0c\u5e76\u4f7f\u7528 ArrayList \u6216 StringBuilder \u7d2f\u79ef\u5b83\u4eec\uff0c\u6700\u540e\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>Scanner scanner = new Scanner(\"Hello World\");\nList&lt;String&gt; words = new ArrayList&lt;&gt;();\nwhile (scanner.hasNext()) {\n    words.add(scanner.next());\n}\nString[] wordsArray = words.toArray(new String[0]); \/\/ [\"Hello\", \"World\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u7528\u4e8e\u5339\u914d\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u3002\u4f7f\u7528 Pattern \u548c Matcher \u7c7b\uff0c\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e0e\u6307\u5b9a\u6a21\u5f0f\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>String[] words = \"Hello World\".split(\"(?&lt;=\\s)\"); \/\/ [\"Hello\", \"World\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5\u7684\u5efa\u8bae\uff1a<\/strong><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u4f8b\u5982\u4f7f\u7528\u7a7a\u683c\u6216\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f7f\u7528 split() \u65b9\u6cd5\u3002<\/li>\n<li>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u62c6\u5206\uff0c\u4f7f\u7528 StringTokenizer \u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li>\u5bf9\u4e8e\u5927\u5b57\u7b26\u4e32\u6216\u9700\u8981\u81ea\u5b9a\u4e49\u5206\u9694\u7b26\uff0c\u4f7f\u7528 Scanner\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava \u5b57\u7b26\u4e32\u600e\u4e48\u5408\u5e76\u6210\u4e00\u4e2a\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>\u5728 java \u4e2d\uff0c\u6709\u56db\u79cd\u65b9\u6cd5\u53ef\u5c06\u5b57\u7b26\u4e32\u5408\u5e76\u6210\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\uff1a\u57fa\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 stringtokenizer\uff1a\u4f7f\u7528\u6307\u5b9a\u7684\u5206\u9694\u7b26\u62c6\u5206\u5b57\u7b26\u4e32\u3002\u4f7f\u7528 scanner\uff1a\u9010\u4e2a\u89e3\u6790\u5b57\u7b26\u4e32\u5355\u8bcd\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff1a\u5339\u914d\u548c\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6a21\u5f0f\u5e76\u521b\u5efa\u6570\u7ec4\u3002\u6839\u636e\u5177\u4f53\u60c5\u51b5\u5efa\u8bae\u9009\u62e9\u9002\u5f53\u7684\u65b9\u6cd5\u3002 \u5982\u4f55\u5c06 Java \u5b57\u7b26\u4e32\u5408\u5e76\u6210\u6570\u7ec4 \u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5408\u5e76\u6210\u4e00\u4e2a\u6570\u7ec4\uff1a 1. \u4f7f\u7528 split() \u65b9\u6cd5 split() \u65b9\u6cd5\u53ef\u57fa\u4e8e\u6307\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62c6\u5206\uff0c\u5f62\u6210\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] words = &#8220;Hello World&#8221;.split(&#8221; &#8220;); \/\/ [&#8220;Hello&#8221;, &#8220;World&#8221;] \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 StringTokenizer StringTokenizer \u7c7b\u53ef\u57fa\u4e8e\u6307\u5b9a\u7684\u5206\u9694\u7b26\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u4ee4\u724c\uff08tokens\uff09\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u679a\u4e3e\u6765\u904d\u5386\u8fd9\u4e9b\u4ee4\u724c\u3002\u53ef\u4ee5\u4f7f\u7528 toArray() \u65b9\u6cd5\u5c06\u4ee4\u724c\u8f6c\u6362\u4e3a\u6570\u7ec4\u3002\u4f8b\u5982\uff1a StringTokenizer tokenizer = new StringTokenizer(&#8220;Hello World&#8221;, &#8221; &#8220;); String[] words = tokenizer.toArray(new String[0]); \/\/ [&#8220;Hello&#8221;, &#8220;World&#8221;] \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-67043","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67043","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=67043"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/67043\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=67043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=67043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=67043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}