Search code examples
coldfusion

coldfusion - remove specific text from string


I am looking to remove "pharmacy" from an input field before I enter the text into the database.

Example - "Ciaran pharmacy" will show as "Ciaran".


Solution

  • Your requirements are pretty simple:

    <cfscript>
        name = "Ciaran pharmacy";
        newName = replace(name, 'pharmacy', '');
    </cfscript>
    

    I suspect there's more complication to it than that.

    • Is it only if 'pharmacy' is the last word?
    • Is it always just the last word in the name?
    • Or is it always 'pharmacy' regardless of where it is in the string?

    If it's the 3rd case, specify the 'ALL' parameter:

    newName = replace(name, 'pharmacy', '', 'ALL');