Sheetal Bhatewara
Sheetal Bhatewara

Reputation: 1607

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

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?

Upvotes: 151

Views: 147562

Answers (11)

Saurabh Rathore
Saurabh Rathore

Reputation: 11

If you are working on spring project then you can use org.springframework.util.StringUtils's startsWithIgnoreCase method.

org.springframework.util.StringUtils.startsWithIgnoreCase(string,prefix);

In this way you do not need any external dependency.

Upvotes: 0

Prasad Kharkar
Prasad Kharkar

Reputation: 13566

You can use

someString.toUpperCase().startsWith(someOtherString.toUpperCase())

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213311

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, needle.length()));  // 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.

Upvotes: 102

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>

Upvotes: 24

A.G.
A.G.

Reputation: 334

You can use String.search() function with regex that will give you the index of the first substring match of the search string. Then you can test the returned index to see if it is 0. 'i' at the end makes the search case insensitive.

const session = 'Session';
const startsWith = session.search(/sEsSi/i) === 0;

Upvotes: 0

TTTT
TTTT

Reputation: 31

myString.StartsWith(anotherString, StringComparison.OrdinalIgnoreCase)

Upvotes: 2

pedram
pedram

Reputation: 1113

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

Upvotes: -4

Nemesis
Nemesis

Reputation: 2918

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

Upvotes: 123

RamonBoza
RamonBoza

Reputation: 9038

You can always do

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

Upvotes: -2

newuser
newuser

Reputation: 8466

try this,

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

Upvotes: 0

agad
agad

Reputation: 2189

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

Upvotes: 0

Related Questions