I have 35+ words that I need to search for within a string. If the string ENDS with any of the 35+ words then I need to remove them. I am using .removeEnd()
and it works great. My issue is my method below only handles one word at a time and I feel like there has got to be a DRY way of writing this method to check for all 35+ words. Does anyone have any suggestions other then regex? I would like to continue to use APEX.
if (Trigger.new[0].Legal_Name_Normal__c.endsWith(' limited liability company')) {
String replacement = 'limited liability company';
newLegal = Trigger.new[0].Legal_Name_Normal__c.removeEnd(replacement);
Trigger.new[0].Legal_Name_Normal__c = newLegal;
}
My initial thought was to do an OR
statement but if I did that, I am not sure how to target each item and save it into the replacement
variable. My guess is I would have to save each word in its own String and then write a conditional around newLegal = Trigger.new[0].Legal_Name_Normal__c.removeEnd(replacement);
Does this seem like the best route?