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

13302
6
Jump to solution
08-28-2020 08:33 AM
ChrisLope
Occasional Contributor

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
1 Solution

Accepted Solutions
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)

View solution in original post

6 Replies
JoeBorgione
MVP Emeritus

There are Left, Mid, and Right functions.

That should just about do it....
ChrisLope
Occasional Contributor

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
Occasional Contributor

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
ChrisLope
Occasional Contributor

Thanks for the answer, @XanderBakker!  I apologize for my delayed "thanks"--I didn't see it until yesterday (4/3/2024) when I came across this post while searching for a solution to the same problem I apparently had 4 years ago, hahaha. 

Once I started fiddling around with things this time, the vague and fuzzy memories of "why is this only working on two sites out of 140ish" came back and I had the eureka moment (again) of realizing that the report that is extracted from the mainframe adds a bunch of spaces/tabs to some of the fields (e.g. "Sitename               " vs. "Sitename").  Back in 2020, I fixed the issue in Excel prior to pulling the sheet in as a table.  Since I'm pulling the sites from a database this time, the dirty fields are still dirty and mucking up my day.

The final "fix" for my problem was:  (dropping it here for when I run into this issue again in 2028, haha)

var txt = Trim($feature.OFC_NM);
return Left(txt,Count(txt)-2);

 

0 Kudos