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 (TheBlogReaders.com) and the last 4 letter can be removed like below
Apex Class:

X = 'TheBlogReaders.com';
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 = 'TheBlogReaders.com,';
strWebsite = strWebsite.removeEnd(',');

Result: TheBlogReaders.com

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

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_removeEnd

 

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 #TheBlogReaders.com%%';
strReplaceSpecialCharacter = strReplaceSpecialCharacter.replaceAll('[^a-zA-Z0-9\\s+]', '');
system.debug('strReplaceSpecialCharacter::'+strReplaceSpecialCharacter);

Result:

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

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

*