2

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?

2 Answers 2

6

This is a great use case for Regular Expressions. One benefit is you can make it case insensitive just by adding (?i) to the beginning of your expression.

List<String> substrings = new List<String>
{
    'limited liability company',
    'some other value',
    'etc.'
};
String expression = '(?i)(' + substrings.join('|') + ')$';
// this expression uses an or join to match any single expression
// followed by the end of the string

// without this terminating $, the expression would be equivalent to
// checking contains, rather than ends with

for (MyObject__c record : trigger.new)
{
    if (record.Legal_Name_Normal__c != null)
    {
        record.Legal_Name_Normal__c = record.Legal_Name_Normal__c.replaceAll(expression, '');
    }
}
1
  • 2
    I normally love to quote Jamie Zawinski in situations involving regexp, but this really is one of the situations where a regexp is not only good, it's the right tool for the job.
    – Derek F
    Commented Oct 24, 2017 at 19:03
5

Use may choose to use String.replaceAll:

String keywordRegex = '(LLC|Inc\\.|Limited Liability Company|\\.com)$';
for( **SomeType** record: Trigger.new) {
    if(record.Legal_Name_Normal__c != null) {
        record.Legal_Name_Normal__c = record.Legal_Name_Normal__c.replaceAll(keywordRegex,'');
    }
}

This will perform a regex against any number of values (separated by the | for "OR" logic), and the "$" at the end matches only at the end of the string.

5
  • Haha, well that's near verbatim.
    – Adrian Larson
    Commented Oct 24, 2017 at 18:30
  • @AdrianLarson You know what they say about great minds, lol.
    – sfdcfox
    Commented Oct 24, 2017 at 18:30
  • @sfdcfox and Adrian- great suggestions. I figured regex would be the easiest with this many records, I was just hoping for a way to do it with APEX methods. I didn't clearly explain the requirements, I just updated them. It is if the string ends with the word. which is why I chose removeEnd(). I am sure I can still specify this with regex.
    – Olivia
    Commented Oct 24, 2017 at 18:38
  • Oh and I just read the end of your answer. You've got the end result covered! Thank you
    – Olivia
    Commented Oct 24, 2017 at 18:40
  • 1
    @Olivia Yes, you could use removeEnd, but it's inefficient compared to a single replaceAll. You might want to take some time to learn how regex works, because it can turn really complicated logic into really simple logic.
    – sfdcfox
    Commented Oct 24, 2017 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .