首页 文章

在Java中,如何检查字符串是否包含子字符串(忽略大小写)? [重复]

提问于
浏览
476

这个问题在这里已有答案:

我有两个 Stringstr1str2 . 如何检查 str1 中是否包含 str2 ,忽略大小写?

6 回答

  • 10
    str1.toLowerCase().contains(str2.toLowerCase())
    
  • 961

    matches() 怎么样?

    String string = "Madam, I am Adam";
    
    // Starts with
    boolean  b = string.startsWith("Mad");  // true
    
    // Ends with
    b = string.endsWith("dam");             // true
    
    // Anywhere
    b = string.indexOf("I am") >= 0;        // true
    
    // To ignore case, regular expressions must be used
    
    // Starts with
    b = string.matches("(?i)mad.*");
    
    // Ends with
    b = string.matches("(?i).*adam");
    
    // Anywhere
    b = string.matches("(?i).*i am.*");
    
  • 6

    如果你能够使用org.apache.commons.lang.StringUtils,我建议使用以下内容:

    String container = "aBcDeFg";
    String content = "dE";
    boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content);
    
  • 29

    您可以使用 toLowerCase() 方法:

    public boolean contains( String haystack, String needle ) {
      haystack = haystack == null ? "" : haystack;
      needle = needle == null ? "" : needle;
    
      // Works, but is not the best.
      //return haystack.toLowerCase().indexOf( needle.toLowerCase() ) > -1
    
      return haystack.toLowerCase().contains( needle.toLowerCase() )
    }
    

    然后用以下方法调用:

    if( contains( str1, str2 ) ) {
      System.out.println( "Found " + str2 + " within " + str1 + "." );
    }
    

    请注意,通过创建自己的方法,您可以重用它 . 然后,当有人指出你应该使用 contains 而不是 indexOf 时,你只需要改变一行代码 .

  • 121

    我也赞成RegEx解决方案 . 代码会更清晰 . 在我知道字符串变得很大的情况下,我会毫不犹豫地使用toLowerCase(),因为字符串是不可变的并且必须被复制 . 此外,matches()解决方案可能会令人困惑,因为它将正则表达式作为参数(搜索“Need $ le”冷却有问题) .

    基于上面的一些例子:

    public boolean containsIgnoreCase( String haystack, String needle ) {
      if(needle.equals(""))
        return true;
      if(haystack == null || needle == null || haystack .equals(""))
        return false; 
    
      Pattern p = Pattern.compile(needle,Pattern.CASE_INSENSITIVE+Pattern.LITERAL);
      Matcher m = p.matcher(haystack);
      return m.find();
    }
    
    example call: 
    
    String needle = "Need$le";
    String haystack = "This is a haystack that might have a need$le in it.";
    if( containsIgnoreCase( haystack, needle) ) {
      System.out.println( "Found " + needle + " within " + haystack + "." );
    }
    

    (注意:根据您的需要,您可能希望以不同的方式处理NULL和空字符串 . 我认为我的方式更接近字符串的Java规范 . )

    速度关键解决方案可以包括通过字符迭代干草堆字符寻找针的第一个字符 . 当第一个字符匹配时(不区分大小写),开始逐个字符地迭代针,在大海捞针中查找相应的字符,如果所有字符都匹配则返回“true” . 如果遇到不匹配的字符,则在下一个字符处通过haystack继续迭代,如果到达> haystack.length() - needle.length(),则返回“false” .

  • 20

    我将使用contains方法和 toUpper 方法的组合,它们是String类的一部分 . 一个例子如下:

    String string1 = "AAABBBCCC"; 
    String string2 = "DDDEEEFFF";
    String searchForThis = "AABB";
    
    System.out.println("Search1="+string1.toUpperCase().contains(searchForThis.toUpperCase()));
    
    System.out.println("Search2="+string2.toUpperCase().contains(searchForThis.toUpperCase()));
    

    这将返回:

    Search1 = true Search2 = false

相关问题