Skip to main content
Best answer

The best way to handle name splitting with a dreaded middle name (or two!)

  • September 27, 2019
  • 8 replies
  • 1086 views

AndrewJDavison_Luhhu
Forum|alt.badge.img+10

Name splitting should be pretty simply.

The quickest way would be to add a 'Formatter -> Split step'... get the first and second.

But, what if there is a middle name... first and last gets your those, but how to get the middle?

Well you could output as 'line items (separate fields)' and grab the second field.

But then what if there are two middle names?

It's an edge case for sure, but I like to build workflows that account for all possibilities. I'm guessing there is a Code step or some other creative uses of a Formatter step that would allow me to identify one or two (or more) middle names where they exist and isolate them as fields I can use in action steps.

Any ideas?


Best answer by Metagrate_John4manBest answer by Metagrate_John4man

Hi @Luhhu !

It's funny how things come up here very similar to similar to something I've worked on recently. And to me, Code by Zapier is almost always the answer 😉

var firstName = '';

var middleName = '';

var lastName = '';

var fullName = inputData.fullName.trim();

var arrFullName = fullName.trim().split(' ');

var numNames = arrFullName.length;

if (fullName == '') { numNames = 0; } //only needed to reflect fullName is blank

switch (numNames) {

   case 0:

        break;

   case 1:

       firstName = arrFullName[0];

       break;

   case 2:

       firstName = arrFullName[0];

       lastName = arrFullName[1];

       break;

   default:

       firstName = arrFullName[0];

       lastName = arrFullName[numNames - 1];

       var i;

       for (i = 1; i < numNames - 1; i++) {

           middleName += arrFullName[i] + ' ';

       }

       middleName = middleName.trim();

}

output = [{ firstName, middleName, lastName }];

So using your name as an example... wait, what is your name? Hmm, I seem to recall you're from across the pond, I have a theory ... here are sample results:

inputData.fullName:

numNames: 0

firstName:

middleName:

lastName:

inputData.fullName: Luhhu

numNames: 1

firstName: Luhhu

middleName:

lastName:

inputData.fullName: Luhhu Who

numNames: 2

firstName: Luhhu

middleName:

lastName: Who

inputData.fullName: Doctor Luhhu Who

numNames: 3

firstName: Doctor

middleName: Luhhu

lastName: Who

inputData.fullName: Doctor Lou Who Who

numNames: 4

firstName: Doctor

middleName: Lou Who

lastName: Who

Hope this helps!


View original
Did this topic help you find an answer to your question?

8 replies

Metagrate_John4man
Forum|alt.badge.img+1

Hi @Luhhu !

It's funny how things come up here very similar to similar to something I've worked on recently. And to me, Code by Zapier is almost always the answer 😉

var firstName = '';

var middleName = '';

var lastName = '';

var fullName = inputData.fullName.trim();

var arrFullName = fullName.trim().split(' ');

var numNames = arrFullName.length;

if (fullName == '') { numNames = 0; } //only needed to reflect fullName is blank

switch (numNames) {

   case 0:

        break;

   case 1:

       firstName = arrFullName[0];

       break;

   case 2:

       firstName = arrFullName[0];

       lastName = arrFullName[1];

       break;

   default:

       firstName = arrFullName[0];

       lastName = arrFullName[numNames - 1];

       var i;

       for (i = 1; i < numNames - 1; i++) {

           middleName += arrFullName[i] + ' ';

       }

       middleName = middleName.trim();

}

output = [{ firstName, middleName, lastName }];

So using your name as an example... wait, what is your name? Hmm, I seem to recall you're from across the pond, I have a theory ... here are sample results:

inputData.fullName:

numNames: 0

firstName:

middleName:

lastName:

inputData.fullName: Luhhu

numNames: 1

firstName: Luhhu

middleName:

lastName:

inputData.fullName: Luhhu Who

numNames: 2

firstName: Luhhu

middleName:

lastName: Who

inputData.fullName: Doctor Luhhu Who

numNames: 3

firstName: Doctor

middleName: Luhhu

lastName: Who

inputData.fullName: Doctor Lou Who Who

numNames: 4

firstName: Doctor

middleName: Lou Who

lastName: Who

Hope this helps!



jesse
Forum|alt.badge.img+9
  • Architect
  • 1348 replies
  • September 27, 2019

Hey @Luhhu! Have you seen the "All (as separate fields)" option? That will allow you to split the name into separate fields by a separator of your choosing. That should help you grab all parts of a name, regardless of how many types of names there are. :)

Resultado de imagen para gif example


AndrewJDavison_Luhhu
Forum|alt.badge.img+10

@jesse - Yep, that works well most of the time. In my case "Andrew James Davison" would end up as

first name = item 1

middle name = item 2

last name = item 3

Easy! But, what if my name was "Andrew James Charles Davison" In that case 'David' is going to get mapped as my last name and Davison is going to get dropped completely.

What I'm looking for is something that can handle a variable number of name parts... which @John4man_Metagrate solution does perfectly - thanks you John! (and now you know my name as well)




Metagrate_John4man
Forum|alt.badge.img+1

@Luhhu Glad, it helped, Andrew, and good to meet you! I spent a month in London as a kid and have fond memories.



Danvers
Forum|alt.badge.img+12
  • Zapier Staff
  • 3731 replies
  • September 30, 2019

@Metagrate_John4man great answer - and as another Brit I'll always appreciate a Doctor Who reference 😉


One way to do it without code would be to use 2 separate Formatter - Split text steps. Have the first return the first segment of text and the second return the last segment of text.

Between the two steps you'll have the first and last name (skipping any middle names). It's not as elegant as the code step and it doesn't allow you to also return the middle name for later steps in the Zap, but it's simple to set up 😊



AndrewJDavison_Luhhu
Forum|alt.badge.img+10

@Danvers What would be super useful would be to add "first and last" as an output option for the Formatter -> Split step.



Danvers
Forum|alt.badge.img+12
  • Zapier Staff
  • 3731 replies
  • September 30, 2019

@Andrew_Luhhu ooh, interesting - I've popped that in as a feature request for the Formatter 🙂



  • New
  • 1 reply
  • December 24, 2021
Metagrate_John4man wrote:

Hi @Luhhu !

It's funny how things come up here very similar to similar to something I've worked on recently. And to me, Code by Zapier is almost always the answer 😉

var firstName = '';

var middleName = '';

var lastName = '';

var fullName = inputData.fullName.trim();

var arrFullName = fullName.trim().split(' ');

var numNames = arrFullName.length;

if (fullName == '') { numNames = 0; } //only needed to reflect fullName is blank

switch (numNames) {

   case 0:

        break;

   case 1:

       firstName = arrFullName[0];

       break;

   case 2:

       firstName = arrFullName[0];

       lastName = arrFullName[1];

       break;

   default:

       firstName = arrFullName[0];

       lastName = arrFullName[numNames - 1];

       var i;

       for (i = 1; i < numNames - 1; i++) {

           middleName += arrFullName[i] + ' ';

       }

       middleName = middleName.trim();

}

output = [{ firstName, middleName, lastName }];

So using your name as an example... wait, what is your name? Hmm, I seem to recall you're from across the pond, I have a theory ... here are sample results:

inputData.fullName:

numNames: 0

firstName:

middleName:

lastName:

inputData.fullName: Luhhu

numNames: 1

firstName: Luhhu

middleName:

lastName:

inputData.fullName: Luhhu Who

numNames: 2

firstName: Luhhu

middleName:

lastName: Who

inputData.fullName: Doctor Luhhu Who

numNames: 3

firstName: Doctor

middleName: Luhhu

lastName: Who

inputData.fullName: Doctor Lou Who Who

numNames: 4

firstName: Doctor

middleName: Lou Who

lastName: Who

Hope this helps!

 

I was upgrading that a bit, to also extract titles that are e.g. used in Germany

 

let title = '';
let firstName = '';
let middleName = '';
let lastName = '';
let fullName = inputData.fullName.trim();
const arrFullName = fullName.trim().split(' ');
let nameLength = arrFullName.length;
if (fullName === '') { nameLength = 0; } //only needed to reflect fullName is blank
switch (nameLength) {
  case 0:
    break;
  case 1:
    firstName = arrFullName[0];
    break;
  case 2:
    if (arrFullName[0].toLowerCase() === 'dr.' || arrFullName[0].toLowercase() === 'dr') {
      title = arrFullName[0];
    } else {
      firstName = arrFullName[0];
    }
    lastName = arrFullName[1];
    break;
  default:
    if (arrFullName[0].toLowerCase() === 'dr.' || arrFullName[0].toLowerCase() === 'dr') {
      title = arrFullName[0];
      firstName = arrFullName[1];
      let i;
      for (i = 2; i < nameLength - 1; i++) {
        middleName += arrFullName[i] + ' ';
      }
    } else {
      firstName = arrFullName[0];
      let i;
      for (i = 1; i < nameLength - 1; i++) {
        middleName += arrFullName[i] + ' ';
      }
    }
    lastName = arrFullName[nameLength - 1];
    middleName = middleName.trim();
}
output = [{ title, firstName, middleName, lastName }];