Best answer

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

  • 27 September 2019
  • 8 replies
  • 1001 views

Userlevel 7
Badge +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?


icon

Best answer by Metagrate_John4man 27 September 2019, 16:43

View original

8 replies

Userlevel 4
Badge +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!


Userlevel 7
Badge +9

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

Userlevel 7
Badge +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)



Userlevel 4
Badge +1

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


Userlevel 7
Badge +12

@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 😊


Userlevel 7
Badge +10

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


Userlevel 7
Badge +12

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


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 }];

Reply