5

When i tried to run the following code snippet anonymously in dev console i got different boolean results. Is string method startsWith(String) case-sensitive or not?

String s1 = 'Thomas Inc';
System.debug(s1.startswith('ThoMas'));

It returns false when i changed some letters to upper case. Do i have to convert the source and the comparison string to any of the cases before comparing? Do we have case-insensitive version of this method? Please advice.

2 Answers 2

10

The String.startsWith method is case sensitive.

However there is a String.startsWithIgnoreCase method which as the name notes is case insensitive.

2
  • Hmmmm didn't realize this. I googled salesforce string methods. Came to the page and it has a lot less... salesforce.com/us/developer/docs/dbcom_apex250/Content/…
    – dphil
    Jul 30, 2014 at 2:29
  • @dphil those are the docs for database.com's release, which was APIv25. I don't know why that's the top result for apex docs but it's a pretty old copy of them at this point. Jul 30, 2014 at 5:02
3

Your best bet is doing something like:

String s1 = 'Thomas Inc';
System.debug(s1.toUpperCase().startswith('THOMAS'));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .