Select to view content in your preferred language

Arcade script - Extracting the text from a string after the first space

131
4
Jump to solution
Wednesday
MaryPhillips-C
Emerging Contributor

Hello, 

I have a field that contains a street name with directional prefix for some of the streets (for example NE 1st St, N Cottonwood Ave, Willow St). I would like to extract just the street name without the directional (which may be 1 or 2 characters). I have been trying to write a script that returns the text after the first space, only if there is a space in the first three characters, or else returns the entire text.

I've tried it a couple of times and for both the statement validates, however neither work when executed.

For this script the results return NaN for all features that contained a space in the first three characters:

var addstreet = $feature['L0Parcel_boundaries.situs_street'];
var str3 = Left(addstreet,3);
var street = Trim(Second(Split(addstreet,' ')));
if((Find(' ',str3,0))>-1){
return street;
}else{
return addstreet
}

For the next attempt, the expression shows as valid but when it is executed it says the expression on line 4 is invalid:

var addstreet = $feature['L0Parcel_boundaries.situs_street'];
var str3 = Left(addstreet,3);
var street_array = Split(addstreet,' ');
var str = street_array[1];
if((Find(' ',str3,0))>-1){
return str;
}else{
return addstreet
}

I'm still a relative beginner with Arcade, so any assistance is appreciated.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This will strip off the directional text if there's a space in the first three characters.

var addstreet = $feature['L0Parcel_boundaries.situs_street'];
if (Find(" ", Left(addstreet, 3)) < 0) return addstreet;
return Concatenate(Slice(Split(addstreet, " "), 1), " ");

In line 2, if there is no space in the first three characters, the street name is returned.

Otherwise, in line 3, the string is split into an array, the first array item is removed using the Slice function, and the items in the array are Concatenated into a text string separated by a space.

View solution in original post

4 Replies
CodyPatterson
MVP Regular Contributor

Hey @MaryPhillips-C 

It appears this is happening when it can't find a proper match, so you'll need to throw some error checking in to ensure it can pass those errors and continue, something like this maybe:

var addstreet = $feature['L0Parcel_boundaries.situs_street'];
var str3 = Left(addstreet,3);
var street_array = Split(addstreet,' ');
var str = street_array[1];
// add in the check here to account for the count of this being not null
if(Find(' ', str3, 0) > -1 && Count(street_array) > 1){
return str;
}else{
return addstreet
}

If you want everything after, you can concat it:

var addstreet = $feature['L0Parcel_boundaries.situs_street'];
var str3 = Left(addstreet,3);
var street_array = Split(addstreet,' ');
var str = street_array[1];
if(Find(' ', str3, 0) > -1 && Count(street_array) > 1){
// concat here
return Concatenate(Slice(street_array, 1), ' ');
}else{
return addstreet
}

 

1st would be N Cottonwood Ave to Cottonwood

2nd would be N Cottonwood Ave to Cottonwood Ave

Cody

0 Kudos
MaryPhillips-C
Emerging Contributor

Hi Cody! Thanks for the suggestions! I tried the second option but it gave me an out of bounds error on the 4th line. Not sure what that is about since I'm still building my Arcade skills.

0 Kudos
KenBuja
MVP Esteemed Contributor

This will strip off the directional text if there's a space in the first three characters.

var addstreet = $feature['L0Parcel_boundaries.situs_street'];
if (Find(" ", Left(addstreet, 3)) < 0) return addstreet;
return Concatenate(Slice(Split(addstreet, " "), 1), " ");

In line 2, if there is no space in the first three characters, the street name is returned.

Otherwise, in line 3, the string is split into an array, the first array item is removed using the Slice function, and the items in the array are Concatenated into a text string separated by a space.

MaryPhillips-C
Emerging Contributor

This worked perfectly! Thank you so much! I must have tried a dozen different scripts, but I finally decided it was time to ask for assistance. I appreciate your help!

0 Kudos