Is it possible to determine if a String str1="ABCDEFGHIJKLMNOP"
contains a string pattern strptrn="gHi"
? I wanted to know if that's possible when the characters are case insensitive. If so, how?

- 16,842
- 17
- 45
- 54

- 6,320
- 15
- 44
- 59
5 Answers
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;
}

- 15,979
- 4
- 42
- 63
-
1Thanks. Plenty of other nice stuff in there, such as indexOfIgnoreCase... – vikingsteve Sep 17 '13 at 08:19
-
9That 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.) – Hakanai May 27 '14 at 13:53
-
3org.apache.commons.lang3.StringUtils this package not available in android – Rahul Sonone Jun 09 '16 at 11:21
If you won't go with regex:
"ABCDEFGHIJKLMNOP".toLowerCase().contains("gHi".toLowerCase())

- 24,152
- 13
- 73
- 111
-
3While this could be an answer, I don't think it's a good solution on big `String`s – Luiggi Mendoza Dec 24 '12 at 07:41
-
31This doesn't work in the wide world of unicode - see http://stackoverflow.com/a/6996550/372926 – SamStephens May 03 '13 at 19:15
-
1Like `if (file.getName().toLowerCase() .contains(editText.getText().toString().toLowerCase())) ` – Iman Marashi Mar 30 '15 at 03:25
-
-
@SamStephens , toLowerCase() of unicode will work in java7+. in your link you can see comments for the reasons.. – Li3ro Oct 22 '15 at 14:19
-
2It lowercases correctly. But that doesn't mean this comparison works for all cultures. See http://www.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. – SamStephens Oct 23 '15 at 14:50
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();

- 22,654
- 47
- 125
- 190
-
1take a look at the previous answer @SamStephens wrote http://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." – Elad Winkler May 20 '14 at 09:23
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());
}
}
-
1Pass 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. – Rais Alam Dec 24 '12 at 08:07
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.

- 133,369
- 30
- 199
- 275
-
1You should use the bitwise | operator instead of the addition operator. – Greg Ennis Feb 27 '14 at 12:47