Select to view content in your preferred language

Remove extra spaces from string using Arcade

6491
8
Jump to solution
12-20-2019 12:17 PM
JeffThomasILM
Frequent Contributor

I'm trying to calculate a field by concatenating several other fields. I need to remove extra spaces from the concatenated string. There's a straightforward way to do this in Python:

" ".join(concatVar.split())‍‍‍‍‍

I'm trying to do the equivalent thing using Arcade, since attribute rules require it. I've played around with Arcade's split and concatenate and even replace, the latter of which can get the job done but isn't ideal. This seems like the most literal equivalent of the Python function:

concatenate(split(concatVar," ")," ")

The split and repeated concatenation is pointless; it still doesn't get rid of the extra spaces. The fundamental problem seems to be that Arcade's split function requires a separator to be specified rather than treating whitespace of any amount as a separator by default like Python.

I've also thought about creating a for loop to trim each variable and/or use IsEmpty to find and remove nulls before they're even added to the concatenated string, but I thought there must be a simpler way. Thanks for any help.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi jeff.thomasilm ,

I am pretty sure that this is not the best or fastest way, but it would work:

var concatVar = ["this is a test", "    ", null, "let's see if this work", null];
var text1 = Concatenate(concatVar, " ");
var list2 = Split(text1, " ");

var result = "";
for (var i in list2) {
    if (!IsEmpty(list2[i])) {
        if (result == "") {
            result = list2[i];
        } else {
            result += " " + list2[i];
        }
    }
}

return result;

View solution in original post

8 Replies
XanderBakker
Esri Esteemed Contributor

Hi jeff.thomasilm ,

I am pretty sure that this is not the best or fastest way, but it would work:

var concatVar = ["this is a test", "    ", null, "let's see if this work", null];
var text1 = Concatenate(concatVar, " ");
var list2 = Split(text1, " ");

var result = "";
for (var i in list2) {
    if (!IsEmpty(list2[i])) {
        if (result == "") {
            result = list2[i];
        } else {
            result += " " + list2[i];
        }
    }
}

return result;
JeffThomasILM
Frequent Contributor

Xander Bakker,

You're the #1 answerer of my questions on GeoNet. Thank you! You went ahead and created a for loop like I theorized, but would have taken me a long time to figure out! Indeed, it does work. I'm left shaking my head once again at the verbosity of Arcade vs. Python. But, you did concede that it's probably not the best or fastest way. Though I really appreciate the speed of your reply!

XanderBakker
Esri Esteemed Contributor

Hi Jeff Thomas , you're welcome, glad it works, but I am still unsure if this is the fastest way. I am going to see if it is possible to do this without a loop...

XanderBakker
Esri Esteemed Contributor

Hi Jeff Thomas , 

I have tried several other options that are available in normal JavaScript (shift, pop, splice, filter), but none seem to be supported in Arcade. Maybe in the future...

JeffThomasILM
Frequent Contributor

I want to like Arcade, and its use is mandatory for many newer functions of Pro and AGOL, but... it's a young language. There will be bumps. Thank you again for your help, Xander Bakker!

GISAdmin_2008
Emerging Contributor

Perfectly perfect!!!

0 Kudos
feralcatcolonist_old
Frequent Contributor

Is this still the only way to clean excess spaces?

Oh and here's my small contribution of wrapping it up in a function; obviously just a re-mix on the great work of @XanderBakker :

function remove_excess_spaces(input_string) {
    var array_for_input = SPLIT(input_string, " ")
    var result = ""
    for (var item in array_for_input) { 
        if (!ISEMPTY(array_for_input[item])) {
            if (result == "") {
                result = array_for_input[item]
            } else {
                result += " " + array_for_input[item]
            }
        }
    }
    return result
}

 This solved a huge problem for me which involves enforcing some addressing standards without creating constraint rules or otherwise requiring users to manually review work that can be corrected programmatically.

var i = 0
var features = []
function add_value(feature_parameter) {
    if (ISEMPTY(feature_parameter) == false) {
        features[i++] = UPPER(remove_excess_spaces(feature_parameter))
    }
}

add_value($feature.St_PreMod)
add_value($feature.St_PreDir)
add_value($feature.St_PreTyp)
add_value($feature.St_PreSep)
add_value($feature.St_Name)
add_value($feature.St_PosTyp)
add_value($feature.St_PosDir)
add_value($feature.St_PosMod)

var full_name = CONCATENATE(features, " ")

return {
    "result" : {
        "attributes" : {
            "St_PreMod" : UPPER(remove_excess_spaces($feature.St_PreMod)),
            "St_PreTyp" : UPPER(remove_excess_spaces($feature.St_PreTyp)),
            "St_PreSep" : UPPER(remove_excess_spaces($feature.St_PreSep)),
            "St_Name" : UPPER(remove_excess_spaces($feature.St_Name)),
            "St_PosMod" : UPPER(remove_excess_spaces($feature.St_PosMod)),
            "St_FullName" : full_name
        }
    }
}

 As a Pythonista, I'm hoping that we can get something a little less clunky
A comic showing the clumsy way that SQL deals with removing excess spaces.A comic showing the clumsy way that SQL deals with removing excess spaces.


Likes an array of [cats, gardening, photography]
AdminGIS3
New Contributor

I'm new to Pro, and find that some of the processes are clunky.  I used field calculator quickly and easily in the old ArcGIS 10.x with this reference: https://www.esri.com/news/arcuser/0405/files/fieldcalc_1.pdf

0 Kudos