{"id":65998,"date":"2025-05-03T10:17:10","date_gmt":"2025-05-03T02:17:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65998\/"},"modified":"2025-05-03T10:17:10","modified_gmt":"2025-05-03T02:17:10","slug":"java%e6%80%8e%e4%b9%88%e6%9b%bf%e6%8d%a2%e6%95%8f%e6%84%9f%e8%af%8dstring%e6%95%b0%e7%bb%84-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65998\/","title":{"rendered":"Java\u600e\u4e48\u66ff\u6362\u654f\u611f\u8bcdstring\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  \u5728 java \u4e2d\u4f7f\u7528 string \u7c7b\u4e2d\u7684 replaceall \u65b9\u6cd5\u66ff\u6362\u654f\u611f\u8bcd\uff1a\u521d\u59cb\u5316\u654f\u611f\u8bcd\u6570\u7ec4\uff1b\u5c06\u654f\u611f\u8bcd\u8f6c\u6362\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\uff1b\u4f7f\u7528 replaceall \u65b9\u6cd5\u6267\u884c\u66ff\u6362\uff0c\u6307\u5b9a\u66ff\u6362\u8bcd\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/14\/2024111410303066336.jpg\" class=\"aligncenter\" title=\"Java\u600e\u4e48\u66ff\u6362\u654f\u611f\u8bcdstring\u6570\u7ec4\u63d2\u56fe\" alt=\"Java\u600e\u4e48\u66ff\u6362\u654f\u611f\u8bcdstring\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 Java \u66ff\u6362 String \u6570\u7ec4\u4e2d\u7684\u654f\u611f\u8bcd<\/strong><\/p>\n<p>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u6709\u65f6\u9700\u8981\u66ff\u6362\u5176\u4e2d\u7684\u654f\u611f\u8bcd\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 String \u7c7b\u4e2d\u7684 replaceAll \u65b9\u6cd5\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5fc5\u8981\u5305<\/strong><\/li>\n<\/ol>\n<pre>import java.util.regex.Pattern;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u521d\u59cb\u5316\u654f\u611f\u8bcd\u6570\u7ec4<\/strong><\/li>\n<\/ol>\n<pre>String[] sensitiveWords = {\"\u654f\u611f\u8bcd1\", \"\u654f\u611f\u8bcd2\", \"\u654f\u611f\u8bcd3\"};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u5c06\u654f\u611f\u8bcd\u8f6c\u6362\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/li>\n<\/ol>\n<pre>String regex = Pattern.quote(String.join(\"|\", sensitiveWords));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>Pattern.quote() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e49\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u907f\u514d\u7279\u6b8a\u5b57\u7b26\u5e72\u6270\u5339\u914d\u3002String.join() \u65b9\u6cd5\u5c06\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u4e3a\u4e00\u4e2a\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<ol>\n<li><strong>\u6267\u884c\u66ff\u6362<\/strong><\/li>\n<\/ol>\n<pre>String inputString = \"\u6587\u672c\u5305\u542b\u654f\u611f\u8bcd1\u548c\u654f\u611f\u8bcd3\";\nString replacedString = inputString.replaceAll(regex, \"\u66ff\u6362\u8bcd\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>replaceAll \u65b9\u6cd5\u4f7f\u7528\u7ed9\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u654f\u611f\u8bcd\uff0c\u5e76\u5c06\u5176\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u66ff\u6362\u8bcd\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u793a\u4f8b\u8f93\u51fa<\/strong><\/p>\n<pre>replacedString: \u6587\u672c\u5305\u542b\u66ff\u6362\u8bcd\u548c\u66ff\u6362\u8bcd<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava\u600e\u4e48\u66ff\u6362\u654f\u611f\u8bcdstring\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\u4f7f\u7528 string \u7c7b\u4e2d\u7684 replaceall \u65b9\u6cd5\u66ff\u6362\u654f\u611f\u8bcd\uff1a\u521d\u59cb\u5316\u654f\u611f\u8bcd\u6570\u7ec4\uff1b\u5c06\u654f\u611f\u8bcd\u8f6c\u6362\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\uff1b\u4f7f\u7528 replaceall \u65b9\u6cd5\u6267\u884c\u66ff\u6362\uff0c\u6307\u5b9a\u66ff\u6362\u8bcd\u3002 \u4f7f\u7528 Java \u66ff\u6362 String \u6570\u7ec4\u4e2d\u7684\u654f\u611f\u8bcd \u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u6709\u65f6\u9700\u8981\u66ff\u6362\u5176\u4e2d\u7684\u654f\u611f\u8bcd\u3002\u5728 Java \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 String \u7c7b\u4e2d\u7684 replaceAll \u65b9\u6cd5\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\u3002 \u5bfc\u5165\u5fc5\u8981\u5305 import java.util.regex.Pattern; \u767b\u5f55\u540e\u590d\u5236 \u521d\u59cb\u5316\u654f\u611f\u8bcd\u6570\u7ec4 String[] sensitiveWords = {&#8220;\u654f\u611f\u8bcd1&#8221;, &#8220;\u654f\u611f\u8bcd2&#8221;, &#8220;\u654f\u611f\u8bcd3&#8221;}; \u767b\u5f55\u540e\u590d\u5236 \u5c06\u654f\u611f\u8bcd\u8f6c\u6362\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f String regex = Pattern.quote(String.join(&#8220;|&#8221;, sensitiveWords)); \u767b\u5f55\u540e\u590d\u5236 Pattern.quote() \u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e49\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u907f\u514d\u7279\u6b8a\u5b57\u7b26\u5e72\u6270\u5339\u914d\u3002String.join() \u65b9\u6cd5\u5c06\u6570\u7ec4\u4e2d\u7684\u5b57\u7b26\u4e32\u8fde\u63a5\u4e3a\u4e00\u4e2a\u5206\u9694\u7b26\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002 \u6267\u884c\u66ff\u6362 String inputString = &#8220;\u6587\u672c\u5305\u542b\u654f\u611f\u8bcd1\u548c\u654f\u611f\u8bcd3&#8221;; String replacedString = inputString.replaceAll(regex, &#8220;\u66ff\u6362\u8bcd&#8221;); \u767b\u5f55\u540e\u590d\u5236 replaceAll [&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-65998","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65998","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=65998"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65998\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}