How to Reverse a String in Salesforce Apex

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.

  1. 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;
    }
}
  1. 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);
  1. In the execution log window, select the checkbox debug only and then in the debug you will see the original and the reversed string.
Salesforce apex reverse the 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.

  1. 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.
  1. If there are no errors, execute the anonymous window and enter the code below.
String myString = 'Salesforce';
ReverseArrayString.getreverse(myString);
  1. 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.
Reverse a String with Array method in Salesforce Apex

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.

  1. 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);
  1. 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.
Salesforce Apex reverse string using recursion

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.

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.