本文翻译自:In Java, how do I check if a string contains a substring (ignoring case)? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
I have two String
s, str1
and str2
. 我有两个String
, str1
和str2
。 How do I check if str2
is contained within str1
, ignoring case? 如何检查是否str2
被包含在str1
,忽略大小写?
#1楼
参考:https://stackoom.com/question/9Xpc/在Java中-如何检查字符串是否包含子字符串-忽略大小写-重复
#2楼
I also favor the RegEx solution. 我也赞成RegEx解决方案。 The code will be much cleaner. 该代码将更加简洁。 I would hesitate to use toLowerCase() in situations where I knew the strings were going to be large, since strings are immutable and would have to be copied. 在我知道字符串会很大的情况下,我会犹豫使用toLowerCase(),因为字符串是不可变的,必须被复制。 Also, the matches() solution might be confusing because it takes a regular expression as an argument (searching for "Need$le" cold be problematic). 另外,matches()解决方案可能会令人困惑,因为它需要使用正则表达式作为参数(搜索“ Need $ le” Cold会出现问题)。
Building on some of the above examples: 以上述示例为基础:
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 + "." );
}
(Note: you might want to handle NULL and empty strings differently depending on your needs. I think they way I have it is closer to the Java spec for strings.) (注意:根据您的需要,您可能希望以不同的方式处理NULL和空字符串。我认为他们以这种方式更接近Java字符串规范。)
Speed critical solutions could include iterating through the haystack character by character looking for the first character of the needle. 对速度要求严格的解决方案可能包括逐个字符地搜索大海捞针,以寻找针的第一个字符。 When the first character is matched (case insenstively), begin iterating through the needle character by character, looking for the corresponding character in the haystack and returning "true" if all characters get matched. 当第一个字符匹配时(敏感地区分大小写),开始逐个字符地遍历针,在干草堆中查找对应的字符,如果所有字符都匹配,则返回“ true”。 If a non-matched character is encountered, resume iteration through the haystack at the next character, returning "false" if a position > haystack.length() - needle.length() is reached. 如果遇到不匹配的字符,则在下一个字符处通过干草堆继续迭代,如果到达位置> haystack.length()-needle.length(),则返回“ false”。
#3楼
You can use the toLowerCase()
method: 您可以使用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() )
}
Then call it using: 然后使用以下命令调用它:
if( contains( str1, str2 ) ) {
System.out.println( "Found " + str2 + " within " + str1 + "." );
}
Notice that by creating your own method, you can reuse it. 注意,通过创建自己的方法,您可以重用它。 Then, when someone points out that you should use contains
instead of indexOf
, you have only a single line of code to change. 然后,当有人指出您应该使用contains
而不是indexOf
,只需更改一行代码。
#4楼
str1.toLowerCase().contains(str2.toLowerCase())
#5楼
I'd use a combination of the contains method and the toUpper
method that are part of the String class. 我将使用String类的contains方法和toUpper
方法的组合。 An example is below: 下面是一个示例:
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()));
This will return: 这将返回:
Search1=true Search1 = true
Search2=false Search2 = false
#6楼
How about matches()
? 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.*");