Add a Multiple Replace Function in Arcade

218
0
12-01-2022 11:04 AM
Status: Open
jcarlson
MVP Esteemed Contributor

Sometimes I have more than one string of text I want to replace, and with more than one replacement. I would love to have a replacement function similar to what you see in Pandas in Python.

Basically, let users supply a dict of {'old':'new'} values. Here's how I do it using the currently available functions.

function MultiReplace(str, dict){
    var out_str = str
    
    for (var d in dict){
        out_str = Replace(out_str, d, dict[d])
    }
    
    return out_str
}

Which I would use like this:

var in_string = 'text w/ abbreviations & such'

var replacements = {
    'w/' : 'with',
    '&' : 'and',
    'b/c' : 'because'
}

var out_string = MultiReplace(in_string, replacements)

What would be even cooler is to fold this into the standard Replace function, to allow users to do single or multi-replacement based on the parameters supplied.