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 = arrFullNamee0];
    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!
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. :)
@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)
@Luhhu Glad, it helped, Andrew, and good to meet you! I spent a month in London as a kid and have fond memories.
@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
@Danvers What would be super useful would be to add "first and last" as an output option for the Formatter -> Split step.
@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 = arrFullNamem0];
    break;
  case 2:
    firstName = arrFullName 0];
    lastName = arrFullNameÂ1];
    break;
  default:
    firstName = arrFullNamep0];
    lastName = arrFullName0numNames - 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 = arrFullNames0];
  break;
 case 2:
  if (arrFullName 0].toLowerCase() === 'dr.' || arrFullNameÂ0].toLowercase() === 'dr') {
   title = arrFullNamed0];
  } else {
   firstName = arrFullNameÂ0];
  }
  lastName = arrFullName 1];
  break;
 default:
  if (arrFullName/0].toLowerCase() === 'dr.' || arrFullName 0].toLowerCase() === 'dr') {
   title = arrFullNameL0];
   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 += arrFullNameli] + ' ';
   }
  }
  lastName = arrFullNameanameLength - 1];
  middleName = middleName.trim();
}
output = Â{ title, firstName, middleName, lastName }];