Removing the last character from a String Using Salesforce Apex Class

We can easyily remove the last character from a string by using the Substring() method using Apex Class
For Instance, If X is a string ( and the last 4 letter can be removed like below
Apex Class:

X = '';
String Y = X.Substring(0,X.length()-4);

Result: TheBlogReaders

Another Example using “removeEnd”

removeEnd(String substring)    -> Removes the specified substring only if it occurs at the end of the String.
How to remove the last letter or word using Apexclass

string strWebsite = ',';
strWebsite = strWebsite.removeEnd(',');


Reference to removeEnd values from String using Apex Class/Apex Trigger:-


How to Remove the Special Characters using Apex Class/Apex Trigger:

Use replaceAll String Class method to replace the Special Character using Apex Class/Apex Trigger – Replaces each substring of a string that matches the regular expression regExp with the replacement sequence replacement.

replaceAll(regExp, replacement)

String strReplaceSpecialCharacter = '%%Welcome Lightning Web Component! and& Aura Component! %Development \\ / from';
strReplaceSpecialCharacter = strReplaceSpecialCharacter.replaceAll('[^a-zA-Z0-9\\s+]', '');


Written by 

2x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *