New to Arcade, trying to remove some text from a label so it fits better. For example the labels are:
Iowa County
Wood County
Monroe County
How do I write an arcade expression to remove "County" from each label?
Solved! Go to Solution.
This would be my go to solution in the label expression:
Replace($feature.FieldName," County","")
For the reasons mentioned above, I would avoid the Split option.
You could try using split:
Split($feature.labelfield, ' ')
I haven't used this one much, but if I read correctly, the first set of quotes with the space between will tell the Arcade renderer to use spaces as splits. You can also add a comma and then give it an integer value to specify the maximum number of splits.
Split($feature.labelfield, ' ', 1)
That might be helpful because it will for sure remove "County" from the return, but it might give you problems if a county has a two word name (ex. Otter Tail County" would just return "Otter.")
https://developers.arcgis.com/arcade/function-reference/text_functions/#split
Another option might be something like this:
var txt = $feature.labelfield;
return Left(txt, Count(txt)-7)
If all the names are the same (County, which is 6 letters, plus a space before it) this should remove the final 7 characters of each name. This expression came from a similar ask on the community
PS sorry for a bunch of edits, I had ideas right after I hit submit!
Someone else might have a more elegant solution but this should work:
var theInput = 'Alpha Bravo County';
var inputArray = Split(theInput, " ");
var theOutput = '';
for (var i in inputArray) {
if (Upper(inputArray[i]) != 'COUNTY') {
theOutput = theOutput + ' ' + inputArray[i];
}
}
return Trim(theOutput);
I'm using the Upper function just to be safe in case there's any mix of upper / lower / proper case field values (but everyone is always consistent with attribute entry, amirite?)
Steve
Sorry, not on machine to do sample code. Why not just use the text replace function?
Replace('Monroe County', ' County', '') Grab the leading space to County -> _County then replace with empty value
https://developers.arcgis.com/arcade/function-reference/text_functions/#replace
This would be my go to solution in the label expression:
Replace($feature.FieldName," County","")
For the reasons mentioned above, I would avoid the Split option.