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.
- 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);
- 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.

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.
- 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);
- In the execution log window, select the checkbox to debug only. Then, the last character ‘, ‘ is removed from the string.