Skip to main content

Extracting First and Last Name from a Full Name in Deluge

5 min read
Deluge
Zoho
String Manipulation
CRM

When working with contact data in Zoho CRM or other Zoho apps, you often receive a full name as a single string — but your workflow needs the first and last name split into separate fields. A simple split by space breaks immediately when names include prefixes like Dr. or Mr., middle names, or suffixes like Jr. or III.

This post walks through all the tricky scenarios and provides a clean, copy-ready Deluge function to handle them all.


Scenarios to Consider

Before writing any code, let's walk through the edge cases the function needs to handle:

Input NameFirst NameLast NameNotes
John SmithJohnSmithSimple two-part name
Dr. Jane DoeJaneDoePrefix stripped
John Michael SmithJohnSmithMiddle name ignored
Robert Johnson Jr.RobertJohnsonSuffix stripped
Mrs. Sarah Anne WilliamsSarahWilliamsPrefix + middle name
Mr. James William Brown IIIJamesBrownPrefix + middle + suffix
MadonnaMadonna(empty)Single name only

The Deluge Function

Paste this directly into a Deluge script, a workflow action, or a custom function in Zoho CRM:

// -------------------------------------------------------
// Extract First and Last Name from a Full Name in Deluge
// -------------------------------------------------------

// Define known prefixes and suffixes
prefixList = {"Mr.", "Mrs.", "Ms.", "Miss", "Dr.", "Prof.", "Rev.", "Sir"};
suffixList = {"Jr.", "Sr.", "II", "III", "IV", "V", "Esq."};

// --- Replace "" with your actual field or variable ---
fullName = "";

// Trim extra whitespace
cleanName = fullName.trim();

// Split the name into individual parts by space
nameParts = cleanName.toList(" ");

// Build a clean working list (removes empty strings caused by extra spaces)
workingParts = List();
for each part in nameParts
{
    if (part.trim() != "")
    {
        workingParts.add(part.trim());
    }
}

// Strip prefix if the first part is a known prefix
if (workingParts.size() > 0 && prefixList.contains(workingParts.get(0)))
{
    workingParts.remove(0);
}

// Strip suffix if the last part is a known suffix
lastIdx = workingParts.size() - 1;
if (workingParts.size() > 0 && suffixList.contains(workingParts.get(lastIdx)))
{
    workingParts.remove(lastIdx);
}

// Extract first and last name from the remaining parts
firstName = "";
lastName = "";

if (workingParts.size() >= 2)
{
    firstName = workingParts.get(0);
    lastName  = workingParts.get(workingParts.size() - 1);
}
else if (workingParts.size() == 1)
{
    firstName = workingParts.get(0);
}

// Output — replace with field updates or return values as needed
info "First Name: " + firstName;
info "Last Name:  " + lastName;

How It Works

Step 1 — Split the name
toList(" ") breaks the full name string into a list of parts by space. A cleanup loop then removes any empty strings caused by extra whitespace.

Step 2 — Strip the prefix
The function checks whether the first element is in the prefixList. If it is, it removes it from position 0.

Step 3 — Strip the suffix
The function then checks the last element against suffixList. If it matches, it removes it from the end. This happens after the prefix check so the index is correctly recalculated.

Step 4 — Assign first and last name

  • If two or more parts remain, first name is get(0) and last name is get(size - 1) — any middle names in between are safely ignored.
  • If only one part remains (a single name like "Madonna"), it is assigned as the first name and last name is left empty.

Extending the Lists

To support additional prefixes or suffixes, simply add them to the respective lists at the top of the script:

// Add more prefixes as needed
prefixList = {"Mr.", "Mrs.", "Ms.", "Miss", "Dr.", "Prof.", "Rev.", "Sir", "Capt.", "Lt.", "Hon."};

// Add more suffixes as needed
suffixList = {"Jr.", "Sr.", "II", "III", "IV", "V", "Esq.", "MD", "PhD", "DDS", "Ret."};

Feel free to edit the script to fit your needs, and don't hesitate to reach out if you want it updated!

Written by Claude (AI Assistant) · Reviewed and Edited by Melnar Ancit Cordova