How to Remove First and Last Character from a String in Salesforce Apex

Recently, our Salesforce org launched an email campaign, and I had to modify the emails to address the recipients by their first names. However, the first names in the database often include a blank space.

So, I had to remove that space from the last string. To do that, I used the built-in apex function removeEnd() to remove the last character of the string.

In this Salesforce tutorial, I will explain how to remove the first and last character from a String in Salesforce Apex.

Remove First Character from a String in Salesforce Apex

In Salesforce Apex, there are no built-in methods to remove the first character of a string, such as removeEnd(), which we use to remove the last character from a string.

We can achieve the output by removing the first character of the string using the substring() method.

Now, follow the steps below to remove the first character from a string in Salesforce Apex.

  1. In the Salesforce developer console, launch the anonymous window from “Ctrl+E.”
    • Enter the code below in the anonymous window, and enter the code below in that.
String myString = 'Apex development';
String modifiedString = myString.substring(1);

System.debug('Modified String: ' + modifiedString);
  1. In the above code, substring(1) extracts the string starting from the second character (index 1), removing the first character.
    • To debug the anonymous window code, click the Execute button.
    • In the execution log window, select the checkbox to debug only. Then, you will see the string’s first character removed in the output.
Remove first character from string in Salesforce Apex

Following the above steps allows us to remove the first character from a string in Salesforce Apex.

Remove Last Character from a String in Salesforce Apex

To remove the last character from a string in Salesforce Apex, navigate to the Salesforce developer console and follow the steps below.

  1. Open the anonymous apex window in the Salesforce developer console with the keys “Ctrl+E.”
    • Enter the code below in the anonymous window and click the Execute button.
string strEmail = 'edwin@mail.com, ';
strEmail = strEmail.removeEnd(', ');
system.debug(strEmail);
  1. In the execution log window, select the checkbox to debug only. Then, the last character ‘, ‘ is removed from the string.
Salesforce Apex remove last character from string

This way, we can remove the last character from the string in Salesforce Apex using the removeEnd() function.

Remove Last Character from a String Ignoring Case in Salesforce Apex

In Salesforce Apex, we can remove the last character from a string by ignoring the cases using the removeEndIgnoreCase(substring) function.

To know how to remove the last character from a String, ignoring case in Salesforce Apex, navigate to the Salesforce developer console and follow the steps below.

  1. In the Salesforce developer console, launch the anonymous window with the keys “Ctrl+E.
    • In the anonymous code window, enter the code below and click the Execute button.
string strName = 'nathan@mail.com';
strName = strName.removeEndIgnoreCase('.COM');
system.debug(strName);
  1. In the above code, the string has the character ‘.com‘, but in the remove function, we have entered ‘.COM‘. Here, removeEndIgnoreCase(substring) will ignore the case format and remove that part from the string.
    • In the execution log window, select the debug-only checkbox, and you will see the character removed from the string.
Remove last character from string in Salesforce Apex

This way, we can remove the last characters from the string by using the removeEndIgnoreCase (substring) function, which ignores case in Salesforce Apex.

Remove the ‘n’ Number of Characters from a String in Salesforce Apex

To remove the ‘n’ number of characters in Salesforce Apex, we can use the substring method.

Now, navigate to the Salesforce developer console and follow the steps below.

  1. In the Salesforce developer console, execute the anonymous Apex window with “Ctrl+E“.
    • Enter the code below in the Apex anonymous window, and click on the ‘Execute‘ button.
String sentenceString = 'Salesforce Apex development';
Integer n = 5; 

if(n <= sentenceString.length()) {
    String modifiedString = sentenceString.substring(0, sentenceString.length() - n);
    System.debug('Modified String: ' + modifiedString);
} else {
    System.debug('Error: n is greater than the string length');
}
  1. In the above code, n is the number of characters to be removed from the string.
    • The substring(0, length() – n) method takes the part of the string from the beginning (0) to the length minus n, removing the last n characters.
    • In the execution log window, select the checkbox to debug only. Then, in the debug window, you will see that the last n(5) numbers of characters are removed from the string.
Remove n characters from a string in Salesforce apex

This way, we can remove the last n characters of a string in Salesforce Apex.

Remove a Character at a Specified Position of a String in Apex

In Salesforce Apex, we can remove a character from a string using the removeChar() method.

Follow the steps below to remove a character at a specified position in Salesforce Apex.

  1. Launch the anonymous apex window in the developer console with “Ctrl+E.”
    • Enter the code below in the anonymous window and click the Execute button.
static String removeChar(String source, Integer index) {
    return source.left(index) + source.right(source.length() - index - 1);
}

String sourceString = 'Salesforce Apex development';
Integer indexToRemove = 5;

String result = removeChar(sourceString, indexToRemove);

System.debug('Original String: ' + sourceString);
System.debug('Index to Remove: ' + indexToRemove);
System.debug('Updated String: ' + result);
  • In the above code, the static method removeChar is defined as removing a character from a string at a specific index.
  • The method accepts two parameters: the original string (source) and the index (index) of the character to be removed.
  • It uses left() to extract the part of the string before the index and right() to extract the part after the index.
  • Later, it concatenates these two parts to form a new string without the character at the specified index.
  • The method is referred to with the string ‘Salesforce Apex development’ and index 5, removing the character and returning the result ‘Salesforce Apex development.’
  1. In the execution log window, select the ‘Debug only‘ checkbox, and then you will see the output of removing the string character in the debug.
Remove specific string character in Salesforce Apex

In this way, we can remove a specific character from a string in Salesforce Apex by following the steps outlined above.

Conclusion

In this Salesforce tutorial, we have learned to remove the first character of a string using the substring() method, the last character from a string using the removeEnd() method, and the last character of a string, ignoring the upper- and lower-case, using the removeEndIgnoreCase() method.

We also discussed the methods to remove the ‘n’ Number of characters and specific characters from strings in Salesforce Apex.

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.