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?
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;
}
-
1Thanks. Plenty of other nice stuff in there, such as indexOfIgnoreCase... Sep 17, 2013 at 8:19
-
8That 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.)– HakanaiMay 27, 2014 at 13:53
-
3org.apache.commons.lang3.StringUtils this package not available in android Jun 9, 2016 at 11:21
If you won't go with regex:
"ABCDEFGHIJKLMNOP".toLowerCase().contains("gHi".toLowerCase())
-
3While this could be an answer, I don't think it's a good solution on big
String
s Dec 24, 2012 at 7:41 -
28This doesn't work in the wide world of unicode - see stackoverflow.com/a/6996550/372926 May 3, 2013 at 19:15
-
1Like
if (file.getName().toLowerCase() .contains(editText.getText().toString().toLowerCase()))
Mar 30, 2015 at 3:25 -
-
2It 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. Oct 23, 2015 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();
-
1take 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."– ViniMay 20, 2014 at 9: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. Dec 24, 2012 at 8: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.
-
1You should use the bitwise | operator instead of the addition operator. Feb 27, 2014 at 12:47