{"id":65506,"date":"2025-05-03T14:08:31","date_gmt":"2025-05-03T06:08:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65506\/"},"modified":"2025-05-03T14:08:31","modified_gmt":"2025-05-03T06:08:31","slug":"java%e4%b8%adstring%e6%80%8e%e4%b9%88%e8%bd%ac%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65506\/","title":{"rendered":"java\u4e2dstring\u600e\u4e48\u8f6c\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u6709\u4e94\u79cd\u65b9\u6cd5\u53ef\u5c06 java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\u5206\u9694\u4f7f\u7528 stringtokenizer \u5206\u5272\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u4f7f\u7528 arrays.aslist() \u521b\u5efa\u5217\u8868\u4f7f\u7528 stream api \u521b\u5efa\u6570\u7ec4\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302333464033.jpg\" class=\"aligncenter\" title=\"java\u4e2dstring\u600e\u4e48\u8f6c\u6570\u7ec4\u63d2\u56fe\" alt=\"java\u4e2dstring\u600e\u4e48\u8f6c\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/p>\n<p>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u53ef\u4ee5\u5728 Java \u4e2d\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 split() \u65b9\u6cd5<\/strong><\/p>\n<pre>String str = \"Hello, world!\";\nString[] arr = str.split(\",\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u628a\u5b57\u7b26\u4e32\u4ee5\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u5206\u5272\u4e3a\u6570\u7ec4\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>2. \u4f7f\u7528 StringTokenizer<\/strong><\/p>\n<pre>String str = \"Hello world\";\nStringTokenizer st = new StringTokenizer(str);\nwhile (st.hasMoreTokens()) {\n  String token = st.nextToken();\n  \/\/ \u5904\u7406 token\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>StringTokenizer \u662f\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u6309\u6307\u5b9a\u7684\u6a21\u5f0f\u6216\u5206\u9694\u7b26\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>3. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<pre>String str = \"123-456-7890\";\nString[] arr = str.split(\"-\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u5206\u5272\u9009\u9879\u3002\u5b83\u4eec\u53ef\u4ee5\u6309\u590d\u6742\u7684\u6a21\u5f0f\u5206\u5272\u5b57\u7b26\u4e32\u3002<\/p>\n<p><strong>4. \u4f7f\u7528 Arrays.asList()<\/strong><\/p>\n<pre>String str = \"Hello, world!\";\nList&lt;String&gt; list = Arrays.asList(str.split(\",\"));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a\u7684\u5217\u8868\u3002<\/p>\n<p><strong>5. \u4f7f\u7528 Stream<\/strong><\/p>\n<pre>String str = \"Hello, world!\";\nString[] arr = str.split(\",\").toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Stream API \u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u6d01\u7684\u65b9\u5f0f\u6765\u521b\u5efa\u6570\u7ec4\u3002<\/p>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u5206\u5272\uff0csplit() \u65b9\u6cd5\u5c31\u8db3\u591f\u4e86\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5206\u5272\uff0cStringTokenizer \u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u4f1a\u66f4\u597d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u4e2dstring\u600e\u4e48\u8f6c\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>\u6709\u4e94\u79cd\u65b9\u6cd5\u53ef\u5c06 java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\uff1a\u4f7f\u7528 split() \u65b9\u6cd5\u5206\u9694\u4f7f\u7528 stringtokenizer \u5206\u5272\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5206\u5272\u4f7f\u7528 arrays.aslist() \u521b\u5efa\u5217\u8868\u4f7f\u7528 stream api \u521b\u5efa\u6570\u7ec4 \u5982\u4f55\u5c06 Java \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4 \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u7ec4\u53ef\u4ee5\u5728 Java \u4e2d\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a 1. \u4f7f\u7528 split() \u65b9\u6cd5 String str = &#8220;Hello, world!&#8221;; String[] arr = str.split(&#8220;,&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u5c06\u628a\u5b57\u7b26\u4e32\u4ee5\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u5206\u5272\u4e3a\u6570\u7ec4\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 2. \u4f7f\u7528 StringTokenizer String str = &#8220;Hello world&#8221;; StringTokenizer st = new StringTokenizer(str); while (st.hasMoreTokens()) { String token = st.nextToken(); \/\/ [&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-65506","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65506","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=65506"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65506\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}