While working as a Salesforce developer, I had to build custom sorting logic for a Salesforce application in which email addresses needed to be sorted based on their domain, but in reverse order.
For this requirement, I used the Salesforce Apex in-built reverse() method. In this Salesforce tutorial, I will explain how to reverse a string in Salesforce Apex using the reverse() method.
The reverse method in Apex reverses the order of characters in a string. It does not take arguments.
Reverse a String in Salesforce Apex
Navigate to the Salesforce developer console and follow the steps below to reverse a string in Salesforce Apex.
- Create an Apex Class in the developer console; select File > New > Apex Class.
- Enter the name of the Apex class and click the OK button. After this, enter the code below in the Apex class.
public class StringReversal {
public static String reverseString(String inputString){
String reversedString = '';
for(Integer i = inputString.length() -1 ; i>=0 ; i--){
reversedString += inputString.substring(i, i + 1);
}
return reversedString;
}
}- If there are no errors in the code, then execute the anonymous code window and enter the code below, and click the Execute button.
String originalString = 'Salesforce Apex';
String reversedString = StringReversal.reverseString(originalString);
System.debug('Original String:' + originalString);
System.debug('Reversed String:' + reversedString);- In the execution log window, select the checkbox debug only and then in the debug you will see the original and the reversed string.

This way, we can reverse a string in Salesforce Apex by following the steps outlined above.
Reverse a String Using the Array Concept in Salesforce Apex
To reverse a string using the array concept in Salesforce Apex, navigate to the Salesforce developer console and follow the steps below.
- In the developer console, create an Apex Class by selecting File> New> Apex Class.
- Enter the code below in the Apex class and save it.
public class ReverseArrayString {
public static void getreverse(String originalString) {
String[] StringList = originalString.split('');
String reversedString = '';
for (Integer i = StringList.size() - 1; i >= 0; i--) {
reversedString += originalString.substring(i, i + 1);
}
System.debug('originalString--> ' + originalString);
System.debug('reversedString--> ' + reversedString);
}
}Let’s break down and understand the above code.
- getreverse is a method to reverse the input string. Then Stringlist will convert the string into an array.
- The for loop will loop through the array in reverse order and build the reversed string.
- If there are no errors, execute the anonymous window and enter the code below.
String myString = 'Salesforce';
ReverseArrayString.getreverse(myString);- In the above code, we enter the string that needs to be reversed and then call the method that will perform the reversal.
- In the execution log window, select the debug-only checkbox. Then, in the debug, you will see the output for the original and reversed string values.

Following the above steps, we can reverse a string in Salesforce Apex using the array concept.
Reverse a String using Recursion in Salesforce Apex
In Salesforce Apex, we have a Recursive method that simply calls itself. We can also reverse a string in Salesforce Apex by using the recursion method.
- In the developer console, execute the anonymous window, enter the code below, and click the Execute button.
public String reverseString(String input) {
if (input.length() == 0) {
return input;
} else {
return reverseString(input.substring(1)) + input.substring(0, 1);
}
}
String input = 'Salesforce';
String reversed = reverseString(input);
System.debug('Original String: ' + input);
System.debug('Reversed String: ' + reversed);- In the execution log window, select the debug-only checkbox, and then you will see the output for the original and the reversed string values.

This way, we can reverse a string using recursion in Salesforce Apex by following the above steps.
Conclusion
In this Salesforce tutorial, we have learned to reverse a String in Apex. We have discussed the possible methods and scenarios for reversing a string in Salesforce Apex. Within the process, we learned to reverse a string with the reverse() method, array, and recursion.
By now, you will be able to reverse the string in your Salesforce Apex code by following the above steps.
You may also like to read.
- Remove First and Last Character from a String in Salesforce Apex
- Replace String in Salesforce Apex
- Find the Length of the String Using Apex in Salesforce
- Truncate String in Salesforce Apex
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.