Removing leading zeroes from integer part of string using Arcade

1075
5
Jump to solution
08-29-2022 08:25 PM
GraemeBrowning_Aurizon
Occasional Contributor II

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:

  1. left strip the leading zeroes from the number part;
  2. remove the underscore and anything left of that; and
  3. make the "KM" part lowercase.

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.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

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"

Have a great day!
Johannes

View solution in original post

5 Replies
GraemeBrowning_Aurizon
Occasional Contributor II

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.

0 Kudos
JohannesLindner
MVP Frequent Contributor

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"

Have a great day!
Johannes
JohannesLindner
MVP Frequent Contributor

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"

Have a great day!
Johannes
DanPatterson
MVP Esteemed Contributor

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


... sort of retired...
0 Kudos
JohannesLindner
MVP Frequent Contributor

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.


Have a great day!
Johannes