【问题标题】:In Java, how do I check if a string contains a substring (ignoring case)? [duplicate]在 Java 中,如何检查字符串是否包含子字符串(忽略大小写)? [复制]
【发布时间】:2011-01-17 12:22:08
【问题描述】:

我有两个Strings、str1str2。如何检查str2 是否包含在str1 中,忽略大小写?

【问题讨论】:

  • indexOf 和 contains 都是逐个字符的,所以如果你需要更快的字符串搜索(你可以得到),那么你需要实现许多已发布的算法之一。
  • 我有同样的问题,这里是答案:) stackoverflow.com/a/86832/621951

标签: java string substring


【解决方案1】:
str1.toUpperCase().contains(str2.toUpperCase())

更新:

原来的答案是使用toLowerCase() 方法。但正如一些人正确地注意到的那样,Unicode 中存在一些例外情况,最好使用toUpperCase()。因为:

对于一种大写变体,有些语言知道不止一种小写变体。

【讨论】:

  • 由于某种原因,当我调用 "2014-03-25T17:55:00".contains("T")
  • 是否有返回和定位包含 (str2) 的函数?
  • @RadijatoR 是的,它被称为 indexOf,如 int pos = str1.indexOf(str2) 或不区分大小写,如 int pos = str1.toLowerCase().indexOf(str2.toLowerCase())
  • 现在似乎可以工作了。不知道之前发生了什么。相同的代码。
  • @JeremyList ideone.com/weYI1e 它打印 true
【解决方案2】:

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.*");

【讨论】:

  • 您的“indexOf”示例应该使用 >= 0,而不是 > 0,因为如果子字符串出现在字符串的开头,则 0 是有效的。 (在您的示例中没有,但在其他情况下可以。)添加此响应,因为人们显然仍在搜索并找到此答案。
  • 很好的答案!这真的很有用!
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    您可以使用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 时,您只需更改一行代码。

    【讨论】:

    • 记得添加Javadoc关于传递空对象时的行为。
    【解决方案5】:

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

    以上述一些示例为基础:

    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”。

    【讨论】:

    • 我愿意:Pattern.CASE_INSENSITIVE|Pattern.LITERAL
    • @mikejones 我如何才能只检查单词?考虑下面的情况 1) “这是一个干草堆,里面可能有针。”; 2)“这是一个干草堆,里面可能需要$lesss。”;我只希望匹配案例 1,因为在这种情况下需要 $le 作为单词存在。我不希望匹配第二种情况。我怎样才能做到这一点?
    【解决方案6】:

    我将使用包含方法和 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()));
    

    这将返回:

    搜索1=真
    搜索2=false

    【讨论】:

    • 行不通。一些奇怪的国际字符在转换为小写/大写时会转换为多个字符。例如:"ß".toUpperCase().equals("SS")
    • 那会算的。这就是双 s 用德语书写的方式。
    猜你喜欢
    • 2015-07-18
    • 2020-06-13
    • 2016-03-01
    • 2016-08-22
    • 2012-12-10
    • 2013-05-12
    • 2011-03-29
    • 2011-03-31
    • 2020-11-05
    相关资源
    最近更新 更多