function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Reza SarwariReza Sarwari 

Compare 2 dynamic strings

Hello,

i know that should be pretty easy but i can't go ahead.

I have to strings

String1= abcdefg

String2= abcdeXX

 

Now I need a function which returns true If the first 5 letters of 2 strings are the same returns a boolean.

Is there any way i can do this?

I already read

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm

but i didnt find it.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can use the startsWith function to check if a string starts with a particular pattern.  Substring will allow you to get at the first 5 letters, so the following should do it:

 

string1.startsWith(string2.substring(0,5));