Hello! I am hoping I post this question in the right place. I am wanting to write an arcade script that would split one field collected in Survey123 into a first and last name.
I have attached a flow chart of how I anticipate this to work, my question is how to create variables for each word of the split (before and after the space).
Any help or other solutions are appreciated!
Solved! Go to Solution.
Split outputs an array, with each item in its own index in the array. So for your example, Split('Jane Doe', ' ') will output
['Jane', 'Doe']
Individual elements can be accessed by their index:
var name_array = Split('full_name_field')
var first_name = name_array[0]
var last_name = name_array[1]
Do be aware, this assumes a consistent entry of first name, last name as single words. My wife's maiden name, though, would throw this: "van Loon".
Split outputs an array, with each item in its own index in the array. So for your example, Split('Jane Doe', ' ') will output
['Jane', 'Doe']
Individual elements can be accessed by their index:
var name_array = Split('full_name_field')
var first_name = name_array[0]
var last_name = name_array[1]
Do be aware, this assumes a consistent entry of first name, last name as single words. My wife's maiden name, though, would throw this: "van Loon".
Thank you! This is super helpful. For my current need, I only need to get the first name, so I am not too concerned about splitting any last names into two.
However, for future knowledge, it looks like the limit parameter might be able to stop a last name from splitting. In case you wanted to keep a last name with a space together, could the code look like this and still split on the first instance of a space?
var name_array = Split('full_name_field', ' ', 1)
True, but suppose someone had a space in their first name? But yes, the limit parameter can be used that way.
The number limiter means it will only return that many split items, so that would return just the first name in the array.
Ah! Good to know! I admittedly have never used that parameter.
The output is an array of strings. Here's how to safely extract that data assuming arbitrary input:
var tokens = Split("Your Data", " ");
var count = Count(tokens);
var first = null;
var last = null;
if (count == 1) {
first = tokens[0];
} else if (count == 2) {
first = tokens[0];
last = tokens[1];
} else if (count > 2) {
var first_array = [];
for (var i in tokens) {
if (i == count - 1) {
break;
}
Push(first_array, tokens[i]);
}
first = Concatenate(first_array, " ");
last = tokens[-1];
}
// Do what you need with first and last
Ah, looks like I was beaten to the punch! As Josh pointed out, names with more than 1 space in them will lead to issues. My code dumps everything but the last token in the first name slot, with some tweaking you can get everything but the first token in the last name slot.
If the first name is only one word, you can use this to get any last name, regardless of how many words it has.
var test = 'Jane van der Doe'
var firstName = Split(test,' ')[0]
var lastName = Replace(test, firstName + ' ', '')
return lastName + ', ' + firstName
which returns "van der Doe, Jane"