{"id":65509,"date":"2025-05-03T16:17:10","date_gmt":"2025-05-03T08:17:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65509\/"},"modified":"2025-05-03T16:17:10","modified_gmt":"2025-05-03T08:17:10","slug":"java%e6%80%8e%e4%b9%88%e6%8a%8a%e6%95%b0%e7%bb%84%e6%88%aa%e5%8f%96%e6%95%b0%e5%ad%97-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65509\/","title":{"rendered":"java\u600e\u4e48\u628a\u6570\u7ec4\u622a\u53d6\u6570\u5b57"},"content":{"rendered":"<blockquote><p>\n  \u6709\u4e24\u79cd\u65b9\u6cd5\u4ece java \u6570\u7ec4\u4e2d\u622a\u53d6\u6570\u5b57\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u63d0\u53d6\u6570\u5b57\u3002\u4f7f\u7528 arrays.stream() \u8fc7\u6ee4\u548c\u6620\u5c04\u6570\u5b57\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111302363548896.jpg\" class=\"aligncenter\" title=\"java\u600e\u4e48\u628a\u6570\u7ec4\u622a\u53d6\u6570\u5b57\u63d2\u56fe\" alt=\"java\u600e\u4e48\u628a\u6570\u7ec4\u622a\u53d6\u6570\u5b57\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 Java \u622a\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57<\/strong><\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><br \/>\u4f7f\u7528 Java \u622a\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f (RegEx)<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u6765\u67e5\u627e\u548c\u64cd\u7eb5\u6587\u672c\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 String.matches() \u65b9\u6cd5\u6765\u68c0\u67e5\u6570\u7ec4\u5143\u7d20\u662f\u5426\u5305\u542b\u6570\u5b57\uff0c\u5e76\u4f7f\u7528 String.replaceAll() \u65b9\u6cd5\u63d0\u53d6\u5b83\u4eec\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>String[] arr = new String[] {\"abc\", \"123\", \"def\", \"456\"};\nString[] numbers = new String[arr.length];\n\nfor (int i = 0; i &lt; arr.length; i++) {\n    if (arr[i].matches(\".*\\d+.*\")) {\n        numbers[i] = arr[i].replaceAll(\"[^\\d]\", \"\");\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 Arrays.stream()<\/strong><\/p>\n<p>Java 8 \u5f15\u5165\u4e86 Arrays.stream() \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u60a8\u5bf9\u6570\u7ec4\u8fdb\u884c\u6d41\u64cd\u4f5c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 filter() \u64cd\u4f5c\u8fc7\u6ee4\u51fa\u5305\u542b\u6570\u5b57\u7684\u5143\u7d20\uff0c\u7136\u540e\u4f7f\u7528 map() \u64cd\u4f5c\u63d0\u53d6\u6570\u5b57\u3002<\/p>\n<pre>String[] arr = new String[] {\"abc\", \"123\", \"def\", \"456\"};\nString[] numbers = Arrays.stream(arr)\n                        .filter(e -&gt; e.matches(\".*\\d+.*\"))\n                        .map(e -&gt; e.replaceAll(\"[^\\d]\", \"\"))\n                        .toArray(String[]::new);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u7ec4\u6765\u5b58\u50a8\u63d0\u53d6\u7684\u6570\u5b57\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u4e0d\u540c\u7684\u6570\u5b57\u683c\u5f0f\u3002<\/li>\n<li>\u5982\u679c\u6570\u7ec4\u5143\u7d20\u4e0d\u5305\u542b\u4efb\u4f55\u6570\u5b57\uff0c\u5219\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u65b9\u6cd5\u5c06\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u600e\u4e48\u628a\u6570\u7ec4\u622a\u53d6\u6570\u5b57\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\u4e24\u79cd\u65b9\u6cd5\u4ece java \u6570\u7ec4\u4e2d\u622a\u53d6\u6570\u5b57\uff1a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5e76\u63d0\u53d6\u6570\u5b57\u3002\u4f7f\u7528 arrays.stream() \u8fc7\u6ee4\u548c\u6620\u5c04\u6570\u5b57\u3002 \u5982\u4f55\u4f7f\u7528 Java \u622a\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57 \u89e3\u7b54\uff1a\u4f7f\u7528 Java \u622a\u53d6\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a 1. \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f (RegEx) \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u7528\u6765\u67e5\u627e\u548c\u64cd\u7eb5\u6587\u672c\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 String.matches() \u65b9\u6cd5\u6765\u68c0\u67e5\u6570\u7ec4\u5143\u7d20\u662f\u5426\u5305\u542b\u6570\u5b57\uff0c\u5e76\u4f7f\u7528 String.replaceAll() \u65b9\u6cd5\u63d0\u53d6\u5b83\u4eec\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b String[] arr = new String[] {&#8220;abc&#8221;, &#8220;123&#8221;, &#8220;def&#8221;, &#8220;456&#8221;}; String[] numbers = new String[arr.length]; for (int i = 0; i &lt; arr.length; i++) { if (arr[i].matches(&#8220;.*\\d+.*&#8221;)) { numbers[i] = arr[i].replaceAll(&#8220;[^\\d]&#8221;, &#8220;&#8221;); } } \u767b\u5f55\u540e\u590d\u5236 2. [&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-65509","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65509","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=65509"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65509\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}