{"id":300589,"date":"2024-05-20T17:46:51","date_gmt":"2024-05-20T09:46:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/300589.html"},"modified":"2024-05-20T17:47:03","modified_gmt":"2024-05-20T09:47:03","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8java%e4%b8%ad%e5%ae%9e%e7%8e%b0%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e9%83%a8%e5%88%86%e6%9b%bf%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/300589.html","title":{"rendered":"\u5982\u4f55\u5728java\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u66ff\u6362"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26212746\/4ee2e872-0857-4ef6-8472-9f31f43b9925.webp\" alt=\"\u5982\u4f55\u5728java\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u66ff\u6362\" \/><\/p>\n<p><p>\u5728Java\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u66ff\u6362\u4e3b\u8981\u53ef\u4ee5\u4f9d\u9760<code>String<\/code>\u7c7b\u7684<code>replace()<\/code>\u3001<code>substring()<\/code>\u65b9\u6cd5\u548c<code>StringBuilder<\/code>\u6216<code>StringBuffer<\/code>\u7c7b\u7684<code>replace()<\/code>\u65b9\u6cd5\u3002<strong>\u8fd9\u4e9b\u65b9\u6cd5\u7684\u4f7f\u7528\u5404\u6709\u7279\u70b9\uff0c\u53ef\u4ee5\u7075\u6d3b\u5e94\u5bf9\u4e0d\u540c\u7684\u66ff\u6362\u9700\u6c42<\/strong>\u3002\u4f8b\u5982\uff0c<code>String<\/code>\u7c7b\u7684<code>replace()<\/code>\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u8fdb\u884c\u7b80\u5355\u7684\u3001\u5168\u5c40\u7684\u66ff\u6362\u64cd\u4f5c\u3002\u800c\u5f53\u9700\u8981\u5728\u5b57\u7b26\u4e32\u7684\u6307\u5b9a\u90e8\u5206\u8fdb\u884c\u66ff\u6362\u65f6\uff0c<code>StringBuilder<\/code>\u6216<code>StringBuffer<\/code>\u7684<code>replace()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u529b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001\u4f7f\u7528<code>String<\/code>\u7c7b\u7684<code>replace()<\/code>\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p><code>String<\/code>\u7c7b\u63d0\u4f9b\u7684<code>replace(CharSequence target, CharSequence replacement)<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5168\u5c40\u66ff\u6362\u64cd\u4f5c\uff0c\u5b83\u4f1a\u66ff\u6362\u6389\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5339\u914d\u7684\u5b50\u5b57\u7b26\u4e32\u3002\u5f53\u53ea\u9700\u66ff\u6362\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684\u67d0\u4e2a\u90e8\u5206\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5341\u5206\u6709\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">String originalString = &quot;This is a test. This test is simple.&quot;;<\/p>\n<p>String modifiedString = originalString.replace(&quot;test&quot;, &quot;example&quot;);<\/p>\n<p>System.out.println(modifiedString); \/\/ \u8f93\u51fa: This is a example. This example is simple.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e8c\u3001\u4f7f\u7528<code>substring()<\/code>\u7ed3\u5408<code>+<\/code>\u64cd\u4f5c\u8fdb\u884c\u7279\u5b9a\u90e8\u5206\u66ff\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u8981\u66ff\u6362\u5b57\u7b26\u4e32\u7684\u7279\u5b9a\u90e8\u5206\u800c\u975e\u5168\u5c40\u65f6\uff0c<code>substring()<\/code>\u65b9\u6cd5\u53ef\u7528\u4e8e\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\uff0c\u7136\u540e\u901a\u8fc7\u5b57\u7b26\u4e32\u8fde\u63a5\u64cd\u4f5c\u5b8c\u6210\u66ff\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">String originalString = &quot;This is a test string.&quot;;<\/p>\n<p>int start = originalString.indexOf(&quot;test&quot;);<\/p>\n<p>int end = start + &quot;test&quot;.length();<\/p>\n<p>String modifiedString = originalString.substring(0, start) + &quot;example&quot; + originalString.substring(end);<\/p>\n<p>System.out.println(modifiedString); \/\/ \u8f93\u51fa: This is a example string.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u80fd\u591f\u5b9e\u73b0\u7279\u5b9a\u90e8\u5206\u7684\u66ff\u6362\uff0c\u4f46\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u6216\u8005\u9700\u8981\u591a\u6b21\u66ff\u6362\u65f6\u53ef\u80fd\u6548\u7387\u4e0d\u9ad8\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e09\u3001\u4f7f\u7528<code>StringBuilder<\/code>\u6216<code>StringBuffer<\/code>\u7c7b\u7684<code>replace()<\/code>\u65b9\u6cd5<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u66ff\u6362\u9700\u6c42\uff0c\u6bd4\u5982\u9700\u8981\u9891\u7e41\u4fee\u6539\u5b57\u7b26\u4e32\u7684\u573a\u666f\uff0c\u4f7f\u7528<code>StringBuilder<\/code>\u6216<code>StringBuffer<\/code>\u7684<code>replace(int start, int end, String str)<\/code>\u65b9\u6cd5\u4f1a\u66f4\u5408\u9002\u3002\u8fd9\u4e9b\u7c7b\u63d0\u4f9b\u7684<code>replace()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u76f4\u63a5\u5728\u539f\u6709\u5bf9\u8c61\u4e0a\u8fdb\u884c\u64cd\u4f5c\uff0c\u65e0\u9700\u50cf\u4f7f\u7528<code>String<\/code>\u7684<code>replace()<\/code>\u6216<code>substring()<\/code>\u65b9\u6cd5\u90a3\u6837\u4ea7\u751f\u8bb8\u591a\u4e34\u65f6\u5b57\u7b26\u4e32\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">StringBuilder sb = new StringBuilder(&quot;This is a test string.&quot;);<\/p>\n<p>int start = sb.indexOf(&quot;test&quot;);<\/p>\n<p>int end = start + &quot;test&quot;.length();<\/p>\n<p>sb.replace(start, end, &quot;example&quot;);<\/p>\n<p>System.out.println(sb.toString()); \/\/ \u8f93\u51fa: This is a example string.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>StringBuilder<\/code>\u6216<code>StringBuffer<\/code>\u8fdb\u884c\u66ff\u6362\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u66ff\u6362\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u8fd8\u53ef\u4ee5\u907f\u514d\u56e0\u521b\u5efa\u591a\u4e2a\u4e34\u65f6\u5b57\u7b26\u4e32\u5b9e\u4f8b\u800c\u5bfc\u81f4\u7684\u989d\u5916\u5185\u5b58\u6d88\u8017\u3002<\/p>\n<\/p>\n<p><p><strong>\u56db\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u66f4\u7075\u6d3b\u548c\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u66ff\u6362\u529f\u80fd\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408\u6b63\u5219\u8868\u8fbe\u5f0f\u4f7f\u7528<code>String<\/code>\u7c7b\u7684<code>replaceAll()<\/code>\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u66f4\u4e3a\u590d\u6742\u7684\u66ff\u6362\u903b\u8f91\uff0c\u5982\u6761\u4ef6\u66ff\u6362\u3001\u6a21\u5f0f\u5339\u914d\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-java\">String originalString = &quot;This is a test 123. That was a test 456.&quot;;<\/p>\n<p>String modifiedString = originalString.replaceAll(&quot;test \\\\d{3}&quot;, &quot;example&quot;);<\/p>\n<p>System.out.println(modifiedString); \/\/ \u8f93\u51fa: This is a example. That was a example.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>replaceAll()<\/code>\u65b9\u6cd5\u4f7f\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f<code>test \\\\d{3}<\/code>\u6765\u5339\u914d\u6240\u6709\u5305\u542b\u201ctest\u201d\u540e\u8ddf\u4e09\u4f4d\u6570\u5b57\u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5b83\u4eec\u66ff\u6362\u4e3a\u201cexample\u201d\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cJava\u63d0\u4f9b\u4e86\u591a\u79cd\u5b57\u7b26\u4e32\u66ff\u6362\u65b9\u6cd5\uff0c\u9002\u5e94\u4e0d\u540c\u7684\u4f7f\u7528\u573a\u666f\u548c\u9700\u6c42\u3002<strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u9ad8\u6548\u5730\u5b8c\u6210\u5b57\u7b26\u4e32\u66ff\u6362\u4efb\u52a1<\/strong>\uff0c\u5e76\u786e\u4fdd\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1aJava\u4e2d\u5982\u4f55\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u90e8\u5206\u66ff\u6362\uff1f<\/strong><\/p>\n<p>A\uff1a\u5728Java\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684replace\u6216replaceAll\u65b9\u6cd5\u6765\u5b9e\u73b0\u90e8\u5206\u66ff\u6362\u3002replace\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u88ab\u66ff\u6362\u7684\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u8981\u66ff\u6362\u6210\u7684\u65b0\u5b57\u7b26\u4e32\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528replace\u65b9\u6cd5\u5c06\u5236\u8868\u7b26\u66ff\u6362\u4e3a\u7a7a\u683c\uff0c\u6216\u662f\u5c06\u67d0\u4e2a\u7279\u5b9a\u7684\u8bcd\u6c47\u66ff\u6362\u6210\u53e6\u4e00\u4e2a\u8bcd\u6c47\u3002<\/p>\n<p><strong>Q\uff1a\u5728Java\u4e2d\u5982\u4f55\u5b9e\u73b0\u5bf9\u5b57\u7b26\u4e32\u7684\u591a\u6b21\u90e8\u5206\u66ff\u6362\uff1f<\/strong><\/p>\n<p>A\uff1a\u5982\u679c\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u591a\u6b21\u90e8\u5206\u66ff\u6362\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528replaceAll\u65b9\u6cd5\u3002replaceAll\u65b9\u6cd5\u4e0ereplace\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u5b83\u63a5\u53d7\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6b64\u8868\u8fbe\u5f0f\u5c06\u5339\u914d\u60f3\u8981\u66ff\u6362\u7684\u6240\u6709\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u67d0\u4e2a\u6a21\u5f0f\uff0c\u7136\u540e\u5c06\u5176\u66ff\u6362\u6210\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u6bd4\u5982\uff0c\u5c06\u6240\u6709\u7684\u8fde\u7eed\u6570\u5b57\u66ff\u6362\u6210&quot;X&quot;\u3002<\/p>\n<p><strong>Q\uff1a\u5982\u4f55\u5728Java\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u5ffd\u7565\u5927\u5c0f\u5199\u90e8\u5206\u66ff\u6362\uff1f<\/strong><\/p>\n<p>A\uff1a\u5728Java\u4e2d\uff0c\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u66ff\u6362\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\u3002\u5982\u679c\u9700\u8981\u5ffd\u7565\u5927\u5c0f\u5199\u8fdb\u884c\u90e8\u5206\u66ff\u6362\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6807\u5fd7\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u5728replaceAll\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528&quot;(?i)&quot;\u6765\u8868\u793a\u63a5\u4e0b\u6765\u7684\u5339\u914d\u5c06\u5ffd\u7565\u5927\u5c0f\u5199\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u539f\u59cb\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u662f\u5927\u5199\u8fd8\u662f\u5c0f\u5199\uff0c\u90fd\u4f1a\u88ab\u6b63\u786e\u5730\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u65b0\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Java\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u90e8\u5206\u66ff\u6362\u4e3b\u8981\u53ef\u4ee5\u4f9d\u9760String\u7c7b\u7684replace()\u3001substring()\u65b9\u6cd5\u548cS [&hellip;]","protected":false},"author":3,"featured_media":300604,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/300589"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=300589"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/300589\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/300604"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=300589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=300589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=300589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}