Looking for arcade expression to Insert character at specific point in text string

1228
8
Jump to solution
03-06-2023 02:13 PM
RobertChaney
Occasional Contributor

I'm typing in a Number like the following, 1701010000001000 into a PARCELID text field.

This number gets populated into another field (NAME) through a simple calculation attribute rule.

However, what I'd like to see in the NAME field is this, 17-01-01-0-000-001.000

Can anyone share some arcade expression to get me started, my experience with arcade in very limited.

Thanks,

Robert

ArcGIS Pro ver. 3.0.3

 

Tags (3)
0 Kudos
2 Solutions

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

A configurable way:

var parcel_id = Split(Text($feature.PARCELID), "")
//var parcel_id = Split("1701010000001000", "")

var counts = [2, 2, 2, 1, 3, 3, 3]
var separators = ["-", "-", "-", "-", "-", ".", null]

var formatted_id = ""
var index = 0
for(var c in counts) {
    var part = Concatenate(Slice(parcel_id, index, index + counts[c]))
    formatted_id += part + separators[c]
    index += counts[c]
}
return formatted_id

Have a great day!
Johannes

View solution in original post

RobertChaney
Occasional Contributor

Ken,

 Your expression works, if I replace the actual number with $feature.PARCELID, it does what I need.

var test = $feature.PARCELID; //this also work if it's a string (ie "1701010000001000")

return `${Left(test,2)}-${Mid(test,2,2)}-${Mid(test,4,2)}-${Mid(test,6,1)}-${Mid(test,7,3)}-${Mid(test,10,3)}.${Mid(test,13,3)}`

Thank you for your input.

Robert.

 

View solution in original post

0 Kudos
8 Replies
DavidPike
MVP Frequent Contributor

It's no work of art but it may give you an idea of what may be needed.  Assumption that the string length and format never changes.

 

//cast your number to a string
var string = Text($feature.PARCELID)
//make a list of each character in the string
var splitString = Split(string,"")
//shorten variable name for ease of reading
var sS = splitString
//logic to construct output string, incomplete
sS[0] + sS[1] + "-"
+ sS[2] + +sS[3] + "-" 
RobertChaney
Occasional Contributor

David,

 Thanks for your reply.  The expression works fine for labels, however, when adding it to the calculation attribute rule I get a ERROR 002717: Invalid Arcade expression, Arcade error, Index out of bounds, Script line: 8

//cast your number to a string
var string = Text($feature.PARCELID)
//make a list of each character in the string
var splitString = Split(string,"")
//shorten variable name for ease of reading
var sS = splitString
//logic to construct output string, incomplete
sS[0] + sS[1] + "-" + sS[2] + sS[3] + "-" + sS[4] + sS[5] + "-" + sS[6] + "-" + sS[7] + sS[8] + sS[9] + "-" + sS[10] + sS[11] + sS[12] + "." + sS[13] + sS[14] + sS[15]

 

Any ideas?

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

We gave you solutions for parcel ids with exactly 16 characters. Is this always the case?

You also should implement a null check:

if(IsEmpty($feature.PARCELID)) { return null }
// any of our expressions after this.

Have a great day!
Johannes
0 Kudos
RobertChaney
Occasional Contributor

Yes, this field should always contain 16 characters.  I'll implement the null check.

Thanks.

 

0 Kudos
KenBuja
MVP Esteemed Contributor

Another way would be like this, using Template literals and Text functions

var test = 1701010000001000; //this also work if it's a string (ie "1701010000001000")

return `${Left(test,2)}-${Mid(test,2,2)}-${Mid(test,4,2)}-${Mid(test,6,1)}-${Mid(test,7,3)}-${Mid(test,10,3)}.${Mid(test,13,3)}`

 

RobertChaney
Occasional Contributor

Ken,

 Your expression works, if I replace the actual number with $feature.PARCELID, it does what I need.

var test = $feature.PARCELID; //this also work if it's a string (ie "1701010000001000")

return `${Left(test,2)}-${Mid(test,2,2)}-${Mid(test,4,2)}-${Mid(test,6,1)}-${Mid(test,7,3)}-${Mid(test,10,3)}.${Mid(test,13,3)}`

Thank you for your input.

Robert.

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

A configurable way:

var parcel_id = Split(Text($feature.PARCELID), "")
//var parcel_id = Split("1701010000001000", "")

var counts = [2, 2, 2, 1, 3, 3, 3]
var separators = ["-", "-", "-", "-", "-", ".", null]

var formatted_id = ""
var index = 0
for(var c in counts) {
    var part = Concatenate(Slice(parcel_id, index, index + counts[c]))
    formatted_id += part + separators[c]
    index += counts[c]
}
return formatted_id

Have a great day!
Johannes
RobertChaney
Occasional Contributor

Johannes,

 Your expression works as well, thank you for your input.

Robert.

 

0 Kudos