代码之家  ›  专栏  ›  技术社区  ›  Abhishek

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

  •  3
  • Abhishek  · 技术社区  · 6 年前

    我想替换一个字符串中的多个不区分大小写的字符串。

    我可以用: 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
    
    3 回复  |  直到 6 年前
        1
  •  5
  •   Dang Nguyen    6 年前

    你可以试着用 regex 归档

    例子

    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);
    

    结果:

    我的名字叫阿布

    基本理想: Regex ignore case replaceAll()

    从评论@飞行(谢谢)你需要使用

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

    因为regex用不同的含义处理某些特殊字符,例如: . 作为 any single character

    所以使用 Pattern.quote 会这样做。

        2
  •  3
  •   J-Alex Dorota Przeniosło    6 年前

    因为你已经在使用 StringUtils , StringUtils.replaceIgnoreCase 是一个很好的候选人。值得一提的是 3.5+ 必修的。

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

    大小写不敏感地替换另一个字符串中出现的所有字符串 字符串。

    传递给此方法的空引用是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);
    
        3
  •  0
  •   Majid Roustaei    6 年前

    在Java中,字符串是区分大小写的;如果您想使用它们不敏感,则应该使用适当的方法。 例如 equals() 与…完全不同 equalsIgnoreCase() .

    你可以使用:

     String.equalsIgnoreCase()
    

    或:

    compareToIgnoreCase()
    

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