How Do I REMOVE X Characters From the Right Using Arcade?

6845
5
08-28-2020 08:33 AM
ChrisLope
New Contributor II

I finally have a new laptop and it's about to be a new fiscal year, so I am starting off my new GIS project with a clean slate--as in I'm kicking all my kludged together maps and bloated geodatabase with dozens and dozens of orphaned items to the curb.  I have been putting off using Arcade, but since I also plan on publishing more content to our portal, I want to start using it to make life easier for those online maps.

My current dilemma is that I can't figure out how to manipulate a label string the way I need to.  I have a field (Office Name) that includes the City and State.  To make labelling easier, I want to remove the last three characters in that field, which is a space and then the two character state abbreviation.  With Python, I can do it thusly:

def FindLabel ( [OFC_NM] ):
 L = [OFC_NM]
 L = L.title()
 L = L[:-3]
 return L

How  the heck do I this (properly) with Arcade? 

arcade formatting‌ arcade language‌ string.format‌

0 Kudos
5 Replies
JoeBorgione
MVP Esteemed Contributor

There are Left, Mid, and Right functions.

That should just about do it....
ChrisLope
New Contributor II

Thanks for the reply, Joe. Unfortunately those functions don't do what I need to be done, at least not as they are written/documented in the help file.  I actually played around with that prior to my post (should've mentioned that in my original post.)  

Although using the Left function would give me the characters to the left of the string, which is what I need, there is no consistent length to the office names. 

Python deals with this type of situation by allowing you to use negative numbers so using something like 

L = "New Orleans, LA" 
L[:-3]

gives you "New Orleans" when the string in the field is "New Orleans, LA".  I tried using the same logic with Arcade 

Right('New Orleans, LA', -3)

But that does not work and the labels don't actually render.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Chris Lope 

Have a look at this expression based on the suggestion by jborgion 

var txt = 'New Orleans, LA';
return Left(txt, Count(txt)-4);

This will return "New Orleans" (without the comma since it took off 4 characters)

NCESOpen_Data
New Contributor II

This worked perfectly for most of my data, but I need to exclude all points where txt = 'Unavailable'. Is there an easy way to exclude certain values from the expression?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @NCESOpen_Data ,

Sorry for the delay. In your case you can use something like:

var txt = $feature["Your text field"];
if (Lower(txt) != 'unavailable') {
    txt = Left(txt, Count(txt)-4);
}
return txt;

 

A small example:

var list = ['New Orleans, LA', 'Unavailable', 'Redlands, CA', 'Manhattan, NY'];
for (var i in list) {
    var txt = list[i];
    if (Lower(txt) != 'unavailable') {
        txt = Left(txt, Count(txt)-4);
    }
    Console(txt);
}

 

This will write to the console:

New Orleans
Unavailable
Redlands
Manhattan
0 Kudos