Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

The String.startsWith method is case sensitive.

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

share|improve this answer
    
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 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. –  ca_peterson Jul 30 at 5:02

Your best bet is doing something like:

String s1 = 'Thomas Inc';
System.debug(s1.toUpperCase().startswith('THOMAS'));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.