多个不区分大小写的字符串替换

4

我希望能够替换一个字符串中多个不区分大小写的子字符串。

我可以使用以下方法:org.apache.commons.lang.StringUtils.replaceEach(text, searchList, replacementList)

但这只适用于区分大小写的字符串。

是否有类似的方法适用于不区分大小写的字符串?

static String[] old = {"ABHISHEK","Name"};
static String[] nw = {"Abhi","nick name"};
static String s="My name is Abhishek";
System.out.println(StringUtils.replaceEach(s, old, nw));

输出:

My name is Abhishek

预期结果:

My nick name is Abhi

2
有没有类似的方法适用于不区分大小写的字符串?没有。你需要自己编写一个。 - Michael
3个回答

6
您可以尝试使用正则表达式来进行归档。
示例。
String str = "Dang DANG dAng dang";
//replace all dang(ignore case) with Abhiskek
String result = str.replaceAll("(?i)dang", "Abhiskek");
System.out.println("After replacement:" + "   " + result);

结果:

替换后:Abhiskek Abhiskek Abhiskek Abhiskek

编辑


String[] old = {"ABHISHEK","Name"};
String[] nw = {"Abhi","nick name"};
String s="My name is Abhishek";
//make sure old and nw have same size please
for(int i =0; i < old.length; i++) {
    s = s.replaceAll("(?i)"+old[i], nw[i]);
}
System.out.println(s);

结果:

我的昵称是Abhi

基本思想:正则表达式忽略大小写replaceAll()

从评论@flown(谢谢)中,您需要使用

str.replaceAll("(?i)" + Pattern.quote(old[i]), nw[i]);

因为正则表达式会将一些特殊字符解释为不同的含义,例如.表示任意单个字符。所以使用Pattern.quote可以解决这个问题。

我想一次性替换多个字符串。我正在传递字符串数组。 - Abhishek
2
@Ivar,你可以使用str.replaceAll("(?i)" + Pattern.quote(old[i]), nw[i]) - Flown
@DangNguyen:你试过运行这个程序吗?它给我的输出是:“我的昵称是Abhishek”。 - Abhishek
抱歉,我的错。是的,它起作用了。在Java 8中使用流有更好的方法来实现相同的功能吗? - Abhishek

3

既然您已经在使用StringUtils,那么StringUtils.replaceIgnoreCase是一个不错的选择。值得一提的是,需要版本3.5+

public static String replaceIgnoreCase(String text,
                                       String searchString,
                                       String replacement)

Case insensitively replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replaceIgnoreCase(null, *, *)        = null
 StringUtils.replaceIgnoreCase("", *, *)          = ""
 StringUtils.replaceIgnoreCase("any", null, *)    = "any"
 StringUtils.replaceIgnoreCase("any", *, null)    = "any"
 StringUtils.replaceIgnoreCase("any", "", *)      = "any"
 StringUtils.replaceIgnoreCase("aba", "a", null)  = "aba"
 StringUtils.replaceIgnoreCase("abA", "A", "")    = "b"
 StringUtils.replaceIgnoreCase("aba", "A", "z")   = "zbz"
在您的情况下:
String[] old = {"ABHISHEK", "Name"};
String[] nw = {"Abhi", "nick name"};
String s = "My name is Abhishek";

for (int i = 0; i < old.length; i++) {
    s = StringUtils.replaceIgnoreCase(s, old[i], nw[i]);
}

System.out.println(s);

输出:

My nick name is Abhi

如果您经常使用它,甚至可以创建一个辅助方法:

public static String replaceIgnoreCase(final String s, final String searchList[], final String replacementList[]) {
    if (searchList.length != replacementList.length)
        throw new IllegalArgumentException("Search list and replacement list sizes do not match");

    String replaced = s;
    for (int i = 0; i < searchList.length; i++) {
        replaced = StringUtils.replaceIgnoreCase(s, searchList[i], replacementList[i]);
    }

    return replaced;
}

您可以像使用库调用一样使用它:

s = replaceIgnoreCase(s, old, nw);

方法replaceIgnoreCase(String,String,String)对类型StringUtils未定义。 我正在使用commons-lang-2.6.jar - Abhishek

0
在JAVA中,字符串是区分大小写的;如果您想要不区分大小写地使用它们,应该使用适当的方法。 例如equals()equalsIgnoreCase()是非常不同的。
您可以使用:
 String.equalsIgnoreCase()

或者:

compareToIgnoreCase()

如果这些方法返回正确的数据,您可以使用它们来替换您想要的字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接