Select to view content in your preferred language

Arcade Split Function: Split a name into first and last variables

1906
9
Jump to solution
11-01-2023 08:08 AM
Labels (1)
leahmaps
Frequent Contributor

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!

Drawing5 (2).png

 

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

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".

- Josh Carlson
Kendall County GIS

View solution in original post

9 Replies
jcarlson
MVP Esteemed Contributor

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".

- Josh Carlson
Kendall County GIS
leahmaps
Frequent Contributor

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)

 

0 Kudos
jcarlson
MVP Esteemed Contributor

True, but suppose someone had a space in their first name? But yes, the limit parameter can be used that way.

- Josh Carlson
Kendall County GIS
KenBuja
MVP Esteemed Contributor

The number limiter means it will only return that many split items, so that would return just the first name in the array.

split.png

 

jcarlson
MVP Esteemed Contributor

Ah! Good to know! I admittedly have never used that parameter.

- Josh Carlson
Kendall County GIS
0 Kudos
DavidSolari
Frequent Contributor

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
DavidSolari
Frequent Contributor

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.

KenBuja
MVP Esteemed Contributor

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"

leahmaps
Frequent Contributor

 

Thank you, everyone, all your contributions are super helpful! 🙂

@jcarlson @DavidSolari @KenBuja 

 

0 Kudos