Split First and Last Names with Multiple Spaces and Names in Zapier

September 20, 2023
Split First and Last Names with Multiple Spaces and Names in Zapier

This is how you can handle names in Zapier like "John Smith Jr" and split them into "John" and "Smith Jr".

How-To: Accurately Split Complex Names in Zapier

To watch the tutorial, view the video. Otherwise scroll past it to see the text instructions.

Splitting names into 'first' and 'last' can sometimes lead to inaccuracies, especially with multi-space or hyphenated names.

Splitting names, especially those with spaces, in Zapier can be a tricky endeavor. The internet is filled with tutorials that might get you halfway there, but they often stumble when complex names are thrown into the mix.

If you've ever tried to split a name like "John Smith Jr." or even more complex names like "Melissa Smith-Fremont", you'll know the pain of having parts of the name just disappear into the ether. This becomes a real problem, especially when feeding this data into other systems.

Before we dive into our approach, it's worth noting that there are some quality resources out there. One of them is by Jimmy Rose, who offers an excellent tutorial on splitting names into first, middle, and last. Our solution takes inspiration from his method, with a few tweaks for our specific challenge.

The Typical Old Way

To understand our solution, let's first discuss the old approach:

  1. Use the "Formatter by Zapier" step.
  2. Choose 'Text' and then opt for 'Split text'
  3. The output can be First, Last, or into line items'.
  4. This splits the name into separate outputs

This method can be useful for certain use cases, but oftentimes leaves some gaps when trying to move data between systems.

The New, Enhanced Method

Here's where our solution truly shines:

1. Setting the Stage: Set up your input for the step (whether it's the initial trigger or prior action).For our tutorial, let's consider a column called 'full name' from a Google Sheet.

2. The Code Step: Instead of the traditional formatter, we're going to employ the "code by Zapier" step and use JavaScript. Don't fret! Even if you're not a developer, this solution is both simple and elegant.

Copy and paste into a Zapier code step. Make sure to configure the input correctly!

//MAKE SURE TO SET YOUR INPUT IN ZAPIER TO 'name' and the value mapped to the input field with the full name.

var names = input.name.split(' ');
if (names.length > 2) {
  output = [{firstname: names[0], lastname: names.slice(1).join(' ')}];
}
else if (names.length < 2) {
  output = [{firstname: names[0], lastname: ''}];
}
else {
  output = [{firstname: names[0], lastname: names[names.length - 1]}];
}
  • Input: You'd use 'name' as the variable and the value is the input called 'full name' from our Google Sheets. You can just put the value there that you wish to split.
  • Logic: The core logic checks the length of the 'names' array. If it's more than 2 names (indicating mulitple spaces), it captures the first part as the 'first name' and joins the left over array items as the 'last name'. For singular names or just two parts, the script keeps it simple. When there is only one name, it put the name as the "First Name" and assigns a blank value to "Last Name". That helps with later steps to make sure you don't run into errors.

3. Testing: To ensure accuracy, you can test with various names. For instance, for "Melissa Smith-Fremont", the solution correctly splits it into "Melissa" as the first name and "Smith-Fremont" as the last, despite the hyphen.

Conclusion

Hope this was helpful! Code by Zapier steps can open up a lot of possibilities and save tremendous amounts of tasks.

Subscribe to the Profitable Pathways Newsletter

Quarterly Insights into marketing data, attribution, and scaling what works.