Compare two Strings in Salesforce Apex

While working as a Salesforce developer, I was asked to validate the email domains of contacts before saving the records. To ensure that contacts belonging to specific companies are tagged or marked, such as when their email domain matches a predefined list of email domains.

I used the equality operator “==” to compare the strings. In the steps below, I will explain all the possible methods for comparing strings in Salesforce Apex.

Compare Strings in Salesforce Apex

In Salesforce Apex, we have several methods to compare strings efficiently. In the steps below, we will discuss how to compare strings in Apex using different approaches.

Compare Strings using the Operator “==” in Apex

In Salesforce Apex, the most common method for string comparison is using the equality (==) and inequality (!=) operators. These operators check whether two strings are exactly the same or different.

Now, launch the anonymous window in the Salesforce development console, enter the code below, and execute it.

String str1 = 'steven@mail.com';
String str2 = 'steven@mail.com';
String str3 = 'edward@mail.com';

if (str1 == str2) {
    System.debug('str1 and str2 are equal.');
}

if (str1 != str3) {
    System.debug('str1 and str3 are not equal.');
}

In the above code, I have entered similar values for str1 and str2. After this, I have entered an unequal value for str3. In the debug, there will be a comparison between Str1-Str2 and Str1-Str3.

In the execution log window output, str1 is equal to str2, but it is not equal to str3.

This way, we can compare the strings in Apex using the equality operators.

Compare Strings using the equals() method in Apex

In Salesforce Apex, another method to check the string equality is the equals() method. This method compares two strings and considers the case sensitivity.

If the two values are the same but there is a difference in the cases ( uppercase-lowercase), then the strings will be considered unequal.

To compare the string with the equals() method, open the Salesforce developer console, then launch the anonymous Apex window and enter the code below.

String str1 = 'Salesforce';
String str2 = 'salesforce';

if (str1.equals(str2)) {
    System.debug('Strings are equal.');
} else {
    System.debug('Strings are not equal.');
}

Since the comparison is case-sensitive, str1.equals(str2) will return false in the debug because “Salesforce” and “salesforce” have different cases.

How to compare strings in Salesforce Apex

This way, we can compare strings in Salesforce Apex using the equals() when comparing case-sensitive strings.

Compare Strings using the equalsIgnoreCase() method in Apex

In Salesforce Apex, when you compare the strings by ignoring the case of the string, then you can use the apex equalsIgnoreCase() method. This method compares the string irrespective of the case of the string.

Now, open the Salesforce developer console, launch the Apex anonymous window, and enter the code below.

String str1 = 'APEX';
String str2 = 'apex';

if (str1.equalsIgnoreCase(str2)) {
    System.debug('Strings are equal ignoring case.');
} else {
    System.debug('Strings are not equal.');
}

In the execution log window, we will see that output returns as strings are equal despite having differences in case.

How to compare string case in Salesforce Apex

This way, we can compare the string values in Salesforce Apex by ignoring the cases of String.

Compare Strings using the compareTo() method in Apex

In Salesforce Apex, the compareTo() method compares two strings alphabetically. It returns 0 if the strings are equal, a positive number if the first string is greater, and a negative number if the second string is greater.

For example, it considers those words as greater that come before in alphabetical order, like “Apex” will be greater than “Development.”

Now, to compare the strings according to alphabetical order, open the Salesforce developer console and execute the code below in the anonymous Apex window.

String str1 = 'Apex';
String str2 = 'Development';
String str3 = 'Apex';

Integer result1 = str1.compareTo(str2); 
Integer result2 = str1.compareTo(str3); 

System.debug('Comparing str1 and str2: ' + result1);
System.debug('Comparing str1 and str3: ' + result2);

In the execution log window, we will see that the equal string comparison returns output as 0, and for the unequal strings, it will return a negative value, as shown in the output image below.

String comparison in Salesforce Apex

The method will return a negative number since ‘A‘ comes before ‘D‘ alphabetically in comparison to the values ‘Apex‘ and ‘Development.’

In comparing str1 and str3, “Apex” is being compared to “Apex.” Since the two strings are the same, the compareTo() method will return 0.

This way, we can compare strings in their alphabetical order using the compareTo() method in Salesforce Apex.

Conclusion

In this Salesforce Apex tutorial, we learned about string comparison in Apex. We then performed a string comparison using the Apex methods, such as the equality operator “==,” equals(), and equalsIgnoreCase(). Finally, we learned to compare the strings in Apex using the compareTo() method.

You may also like to read.

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.