Home SALESFORCEAPEX Removing the last character from a String Using Salesforce Apex Class

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:
[java]
X = ‘TheBlogReaders.com’;
String Y = X.Substring(0,X.length()-4);
[/java]
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
[java]
string strWebsite = ‘TheBlogReaders.com,’;
strWebsite = strWebsite.removeEnd(‘,’);
[/java]
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)

[JAVA]

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

[/JAVA]

Result:

You may also like

Leave a Comment