How to use `string.startsWith()` method ignoring the case?

JavaString

Java Problem Overview


I want to use string.startsWith() method but ignoring the case.

Suppose I have String "Session" and I use startsWith on "sEsSi" then it should return true.

How can I achieve this?

Java Solutions


Solution 1 - Java

Use toUpperCase() or toLowerCase() to standardise your string before testing it.

Solution 2 - Java

One option is to convert both of them to either lowercase or uppercase:

"Session".toLowerCase().startsWith("sEsSi".toLowerCase());

This is wrong. See: https://stackoverflow.com/a/15518878/14731


Another option is to use String#regionMatches() method, which takes a boolean argument stating whether to do case-sensitive matching or not. You can use it like this:

String haystack = "Session";
String needle = "sEsSi";
System.out.println(haystack.regionMatches(true, 0, needle, 0, 5));  // true

It checks whether the region of needle from index 0 till length 5 is present in haystack starting from index 0 till length 5 or not. The first argument is true, means it will do case-insensitive matching.


And if only you are a big fan of Regex, you can do something like this:

System.out.println(haystack.matches("(?i)" + Pattern.quote(needle) + ".*"));

(?i) embedded flag is for ignore case matching.

Solution 3 - Java

I know I'm late, but what about using StringUtils.startsWithIgnoreCase() from Apache Commons Lang 3 ?

Example :

StringUtils.startsWithIgnoreCase(string, "start");

Just add the following dependency to your pom.xml file (taking the hypothesis that you use Maven) :

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>

Solution 4 - Java

myString.toLowerCase().startsWith(starting.toLowerCase());

Solution 5 - Java

try this,

String session = "Session";
if(session.toLowerCase().startsWith("sEsSi".toLowerCase()))

Solution 6 - Java

You can use someString.toUpperCase().startsWith(someOtherString.toUpperCase())

Solution 7 - Java

StartsWith(String value, bool ignoreCase, CultureInfo? culture) e.g:

string test = "Session";
bool result = test.StartsWith("sEsSi", true, null);
Console.WriteLine(result);

point: in VS by right-clicking on StartsWith and then "pick definition" can see all overloading method

enter image description here

enter image description here

Solution 8 - Java

You can always do

"Session".toLowerCase().startsWith("sEsSi".toLowerCase());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheetal BhatewaraView Question on Stackoverflow
Solution 1 - JavaNemesisView Answer on Stackoverflow
Solution 2 - JavaRohit JainView Answer on Stackoverflow
Solution 3 - JavaSébastien VandammeView Answer on Stackoverflow
Solution 4 - JavaagadView Answer on Stackoverflow
Solution 5 - JavanewuserView Answer on Stackoverflow
Solution 6 - JavaPrasad KharkarView Answer on Stackoverflow
Solution 7 - JavapedramView Answer on Stackoverflow
Solution 8 - JavaRamonBozaView Answer on Stackoverflow