Ignore case for 'contains' for a string in Java
Using "toLowercase" helps:
System.out.println(s.toLowercase(Locale.US).contains("ABCD".toLowercase (Locale.US)));
(of course you could also use toUppercase instead)
You need to convert both the strings to the same case before using contains
s.toLowerCase().contains("ABCD".toLowerCase());
You could use org.apache.commons.lang3.StringUtils.containsIgnoreCase(String, String)
StringUtils.containsIgnoreCase(s, "ABCD")
returns true
Apache documentation here
Not that it would be particularly efficient but you could use a Pattern
matcher to make a case-insensitive match:
Pattern pattern = Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE);
pattern.matcher("ABCD").find();
pattern.matcher("AbcD").find();
Also note that it won't magically solve the locale issue but it will handle it differently than toLowercase(Locale)
, with the conjunction of the Pattern.UNICODE_CASE
flag, it may be able to handle all locales at once.