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.
Solved! Go to Solution.
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;
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;
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!
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...
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...
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!
Perfectly perfect!!!
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
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