One option is to convert both of them to lowercase or uppercase:
"Session".toLowerCase().startsWith("sEsSi".toLowerCase());
Another option is to use the String#regionMatches() method, which takes a boolean argument indicating whether to match case-sensitive or not. You can use it as follows:
String haystack = "Session"; String needle = "sEsSi"; System.out.println(haystack.regionMatches(true, 0, needle, 0, 5));
It checks to see if the needle region from index 0 to length 5 is present in the haystack , starting from index 0 to length 5 or not. The first argument is true , meaning that it will make case insensitive.
And if you are only a big fan of Regex , you can do something like this:
System.out.println(haystack.matches("(?i)" + Pattern.quote(needle) + ".*"));
(?i) built-in flag is used to match cases of ignoring.
Rohit Jain 03 Oct '13 at 8:18 2013-10-03 08:18
source share