Consider:
public static void main(String[] args) { String s = "AbcD"; System.out.println(s.contains("ABCD")); System.out.println(s.contains("AbcD")); }
Output:
false true
I need the result to be true in both cases, regardless of the case. Is it possible?
You need to convert both strings to the same case before using contains
contains
s.toLowerCase().contains("ABCD".toLowerCase());
You can use org.apache.commons.lang3.StringUtils.containsIgnoreCase(String, String)
org.apache.commons.lang3.StringUtils.containsIgnoreCase(String, String)
StringUtils.containsIgnoreCase(s, "ABCD") returns true
StringUtils.containsIgnoreCase(s, "ABCD")
Apache documentation here
You can use Pattern Match to make case insensitive match:
Pattern
Pattern pattern = Pattern.compile(Pattern.quote(s), Pattern.CASE_INSENSITIVE); pattern.matcher("ABCD").find(); pattern.matcher("AbcD").find();
Using "toLowercase" helps:
System.out.println(s.toLowercase(Locale.US).contains("ABCD".toLowercase (Locale.US)));
(of course, you can also use toUppercase)
You can do this with toLowerCase . Something like that:
toLowerCase
s.toLowerCase().contains("aBcd".toLowerCase());
Try the following. It will return 0 if the string matches ...
0
System.out.println(s.compareToIgnoreCase("aBcD"));
It will work fine.