String contains - ignore case
Solution 1
You can use
org.apache.commons.lang3.StringUtils.containsIgnoreCase(CharSequence str,
CharSequence searchStr);
Checks if CharSequence contains a search CharSequence irrespective of case, handling null. Case-insensitivity is defined as by String.equalsIgnoreCase(String).
A null CharSequence will return false.
This one will be better than regex as regex is always expensive in terms of performance.
For official doc, refer to : StringUtils.containsIgnoreCase
Update :
If you are among the ones who
- don't want to use Apache commons library
- don't want to go with the expensive
regex/Pattern
based solutions, - don't want to create additional string object by using
toLowerCase
,
you can implement your own custom containsIgnoreCase
using java.lang.String.regionMatches
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
ignoreCase
: if true, ignores case when comparing characters.
public static boolean containsIgnoreCase(String str, String searchStr) {
if(str == null || searchStr == null) return false;
final int length = searchStr.length();
if (length == 0)
return true;
for (int i = str.length() - length; i >= 0; i--) {
if (str.regionMatches(true, i, searchStr, 0, length))
return true;
}
return false;
}
Solution 2
If you won't go with regex:
"ABCDEFGHIJKLMNOP".toLowerCase().contains("gHi".toLowerCase())
Solution 3
You can use java.util.regex.Pattern with the CASE_INSENSITIVE flag for case insensitive matching:
Pattern.compile(Pattern.quote(strptrn), Pattern.CASE_INSENSITIVE).matcher(str1).find();
Solution 4
Try this
public static void main(String[] args)
{
String original = "ABCDEFGHIJKLMNOPQ";
String tobeChecked = "GHi";
System.out.println(containsString(original, tobeChecked, true));
System.out.println(containsString(original, tobeChecked, false));
}
public static boolean containsString(String original, String tobeChecked, boolean caseSensitive)
{
if (caseSensitive)
{
return original.contains(tobeChecked);
}
else
{
return original.toLowerCase().contains(tobeChecked.toLowerCase());
}
}
Solution 5
An optimized Imran Tariq's version
Pattern.compile(strptrn, Pattern.CASE_INSENSITIVE + Pattern.LITERAL).matcher(str1).find();
Pattern.quote(strptrn) always returns "\Q" + s + "\E" even if there is nothing to quote, concatination spoils performance.
Related videos on Youtube
AlwaysALearner
Updated on July 19, 2022Comments
-
AlwaysALearner almost 2 years
Is it possible to determine if a String
str1="ABCDEFGHIJKLMNOP"
contains a string patternstrptrn="gHi"
? I wanted to know if that's possible when the characters are case insensitive. If so, how? -
Luiggi Mendoza over 11 yearsWhile this could be an answer, I don't think it's a good solution on big
String
s -
Rais Alam over 11 yearsPass true as third parameter if you want value to be checked in case sensitive manner and pass false if you want value to be checked in case insensitive manner.
-
SamStephens almost 11 yearsThis doesn't work in the wide world of unicode - see stackoverflow.com/a/6996550/372926
-
vikingsteve over 10 yearsThanks. Plenty of other nice stuff in there, such as indexOfIgnoreCase...
-
Greg Ennis about 10 yearsYou should use the bitwise | operator instead of the addition operator.
-
Vini almost 10 yearstake a look at the previous answer @SamStephens wrote stackoverflow.com/a/6996550/372926 : you must specify both CASE_INSENSITIVE and UNICODE_CASE, and you still will not get the right values, because while Java uses full casemapping, it uses only simple casefolding. This is a problem."
-
Hakanai almost 10 yearsThat method makes an assumption that the length of the matched part of the haystack will be the same number of UTF-16 code units as the length of the needle. So if you're searching for "ß" and the string contains "SS", it won't find a match, even though these two strings are identical if you ignore the case (in German locale, and of course you do have to set the locale whenever doing this sort of thing.)
-
Iman Marashi about 9 yearsLike
if (file.getName().toLowerCase() .contains(editText.getText().toString().toLowerCase()))
-
pxm almost 9 yearsits true as suggested, regex would always be expensive.
-
Li3ro over 8 years@SamStephens , toLowerCase() of unicode will work in java7+. in your link you can see comments for the reasons..
-
SamStephens over 8 yearsIt lowercases correctly. But that doesn't mean this comparison works for all cultures. See w3.org/International/wiki/Case_folding. They recommend either specifying a culture, or explicitly using a case insentive compare function such as containsIgnoreCase shown above.
-
Rahul Sonone almost 8 yearsorg.apache.commons.lang3.StringUtils this package not available in android