It would be great if it was possible to utilize multiple replace functions in one Arcade expression in ArcGIS Online. Currently, only the last replace function is actually honored and the others are ignored. See example below:
Replace($feature["Gate_Number"], '51 east entrance', 'East Entrance')
Replace($feature["Gate_Number"], '54 middle entrance', 'Middle Entrance')
Replace($feature["Gate_Number"], '1', 'West Entrance')
There are a couple of ways you can do this already. See examples below:
// simply add multiple lines each with one statement
var Gate_Number = "51 east entrance, 1";
Gate_Number = Replace(Gate_Number, '51 east entrance', 'East Entrance');
Gate_Number = Replace(Gate_Number, '54 middle entrance', 'Middle Entrance');
Gate_Number = Replace(Gate_Number, '1', 'West Entrance');
return Gate_Number;
// nest the Replace funcion
var Gate_Number = "51 east entrance, 1";
Gate_Number = Replace(Replace(Replace(Gate_Number, '51 east entrance', 'East Entrance'), '54 middle entrance', 'Middle Entrance'), '1', 'West Entrance');
return Gate_Number;
// create a list with all the search and replace values and loop through them
var Gate_Number = "51 east entrance, 1";
var search_replace = [['51 east entrance', 'East Entrance'],
['54 middle entrance', 'Middle Entrance'],
['1', 'West Entrance']];
for (var i = 0; i < Count(search_replace); i++) {
var search_for = search_replace[i][0];
var replace_with = search_replace[i][1];
Gate_Number = Replace(Gate_Number, search_for, replace_with);
}
return Gate_Number;
Hi Xander,
I am trying to do this as a label expression, therefore each replace statement needs to apply to a label for one of three features. I don't see how the above statements could apply to this.
Thanks,
Erica
If the label text does not contain the string, it will not replace anything. However, the last one that searches for a 1 and replaces (any coincidence) with the other text might cause problems. If the case is that you want to replace the entire (label) text, you shoud not use Replace. Is that the case?
Hi Xander,
Sorry for the very late reply. Yes - it is the case where I want to replace the entire text (label) in each instance. So, the replacements would be:
1. Original text = '51 east entrance', replacement text = 'East Entrance'
2. Original text = '54 middle entrance', replacement text = 'Middle Entrance'
3. Original text = '1', replacement text = 'West Entrance'
You can always do something like this:
var Gate_Number = $feature.Gate_Number;
if (Gate_Number == '51 east entrance') {
Gate_Number = 'East Entrance';
} else if (Gate_Number == '54 middle entrance') {
Gate_Number = 'Middle Entrance';
} else if (Gate_Number == '1') {
Gate_Number = 'West Entrance';
}
return Gate_Number;
... or use the When, Decode or Iff function.
Xander,
It's been awhile, but thank you for the code! That worked perfectly, and I totally appreciate it.
Erica
What about replacing multiple sections of text in one single attribute?
Ex. I want to replace the word "originals" with "" and I want to replace any " " with "%20".
I want to replace the word "originals" with no text and want to replace any spaces with %20 in order to create a valid url when creating a custom expression hyperlink to one of our virtual directory utilizing a source field with just a folder and file name.
Example....
..\originals\Archive\SwrWtr 02of06 87-10.tif
Want the data to look like this
\Archive\SwrWtr%2002of06%2087-10.tif
I've been able to successfully use the Replace function but not against multiple items within the same field.
Any suggestions? Each of these work below separately.... but I want to run multiple Replace functions against the same field $feature.SOURCE.
var a = Replace($feature.SOURCE, "originals", "");
return a;
or
var a = Replace($feature.SOURCE, " ", "%20");
return a;
Figured it out... sharing expression for anyone else wanting to do a replace on multiple strings within one field...
var str = Replace($feature.SOURCE,'originals','');
var myURL = 'http://virtualdirectoryurl/';
myURL = myURL + Replace(str,' ','%20');
return myURL;
Hi AJAMESBAKER_WPB , you may also want to look at the new function "UrlEncode(url)", which will encode any special character into a proper url valid character combination. You can also nest the replace functions in a single statement.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.