I am using ArcGIS Pro 3.0.1 and Arcade.
I have a text field named RouteName with an example value of "XXXX_0045KM" and I need to do three things to its values:
I can do the 2nd and 3rd parts using the code below but is there a way to remove (left strip) any leading zeroes before the number part using Arcade?
var arrName = split($feature.RouteName,"_")
return Lower(arrName[1])
Unfortunately, I cannot just switch to using the Python Parser because I am using this with Attribute-driven symbology—ArcGIS Pro | Documentation as part of labeling measured hatches along lines.
Solved! Go to Solution.
Just had another look at the docs: Number() also supports a format string. So if the part after the underscore is the same for all features, here is another possibility:
Number(Split($feature.RouteName, "_")[1], "####KM") + " km"
The way that I found I could do this is:
var arrName = split($feature.RouteName,"_")
var numKM = Number(Left(arrName[1],4))
return numKM + "km"
The trick was to use Left to get the characters that represented a zfilled integer and convert them to a Number. The Number function stripped off the leading zeroes automatically.
If your values are all formatted the same way, you can also use the Mid() function:
var num = Number(Mid($feature.RouteName, 5, 4))
return num + " km"
If you have differing formats (eg also routes with 5 or 3 digits), you can use the Replace() function:
var txt = Split($feature.RouteName, "_")[1]
var num = Number(Replace(txt, "KM", ""))
return num + " km"
Just had another look at the docs: Number() also supports a format string. So if the part after the underscore is the same for all features, here is another possibility:
Number(Split($feature.RouteName, "_")[1], "####KM") + " km"
does Arcade support python-like string functions?
a
'XXXX_0045KM'
str(int("".join([i for i in a if i.isdigit()])))
'45'
The syntax have to be emulated in Arcade of course
It supports string addition and iterating through a string. You can implement isdigit yourself.
This would be a literal translation:
var a = "XXXX_0045KM"
var digits = []
for(var i in a) {
var ai = Text(Number(a[i]))
if(ai != "NaN") {
Push(digits, ai)
}
}
return Number(Concatenate(digits, "")) + " km"
This would be a good (albeit lengthy) way to extract all digits. In this case, the relevant digits are in defined positions. This would actually return wrong results if there are digits in the XXXX part.
Thank you all this helped me today!