如何在Java中替换不区分大小写的文字子字符串

使用replace(CharSequence target, CharSequence replacement)String中的方法,如何使目标不区分大小写?


例如,它现在的工作方式:


String target = "FooBar";

target.replace("Foo", "") // would return "Bar"


String target = "fooBar";

target.replace("Foo", "") // would return "fooBar"

如何使它替换(或如果有更合适的方法)不区分大小写,以便两个示例都返回“ Bar”?


红颜莎娜
浏览 1332回答 3
3回答

慕尼黑5688855

也许不如其他方法那么优雅,但是它非常扎实且易于遵循,尤其是。对于刚接触Java的人。让我了解String类的一件事是:它已经存在很长时间了,虽然它支持使用regexp进行全局替换和使用Strings(通过CharSequences)进行全局替换,但最后一个没有简单的布尔参数:'isCaseInsensitive'。确实,您曾想过,只需添加一个小开关,就可以避免它的缺失给初学者带来的所有麻烦。现在在JDK 7上,String 仍然不支持这一功能!好吧,反正我会停止抓紧。对于特别喜欢Java的每个人,这里都是您可以剪切并粘贴的deus ex machina。就像我说的那样,它不那么优雅,不会赢得任何出色的编码奖,但是它有效并且可靠。任何意见,请随时贡献。(是的,我知道,StringBuffer可能是管理两个字符串突变行的更好选择,但交换技术很容易。)public String replaceAll(String findtxt, String replacetxt, String str,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; boolean isCaseInsensitive) {&nbsp; &nbsp; if (str == null) {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; if (findtxt == null || findtxt.length() == 0) {&nbsp; &nbsp; &nbsp; &nbsp; return str;&nbsp; &nbsp; }&nbsp; &nbsp; if (findtxt.length() > str.length()) {&nbsp; &nbsp; &nbsp; &nbsp; return str;&nbsp; &nbsp; }&nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; String thesubstr = "";&nbsp; &nbsp; while ((counter < str.length())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && (str.substring(counter).length() >= findtxt.length())) {&nbsp; &nbsp; &nbsp; &nbsp; thesubstr = str.substring(counter, counter + findtxt.length());&nbsp; &nbsp; &nbsp; &nbsp; if (isCaseInsensitive) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thesubstr.equalsIgnoreCase(findtxt)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str = str.substring(0, counter) + replacetxt&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + str.substring(counter + findtxt.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Failing to increment counter by replacetxt.length() leaves you open&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // to an infinite-replacement loop scenario: Go to replace "a" with "aa" but&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // increment counter by only 1 and you'll be replacing 'a's forever.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += replacetxt.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++; // No match so move on to the next character from&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// which to check for a findtxt string match.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (thesubstr.equals(findtxt)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str = str.substring(0, counter) + replacetxt&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + str.substring(counter + findtxt.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter += replacetxt.length();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return str;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java