0

i want to split full name to first ,middle and last in x++ d365fo

str fullName = _contracts.fullName();

// Replace any non-ASCII whitespace characters with regular spaces
fullName = strReplace(fullName, '\u00A0', ' '); // Replace non-breaking spaces with regular spaces
fullName = strReplace(fullName, '\t', ' ');    // Replace tabs with regular spaces

// Trim any leading or trailing whitespace characters from the full name
fullName = strTrim(fullName);


// Split full name into first, middle, and last name
str firstName;
str middleName;
str lastName;

// Find the last space character to split the full name into first and last name
int lastSpace = strFind(fullName, " ", strLen(fullName), -1);
if (lastSpace >= 0)
{
    lastName = subStr(fullName, lastSpace + 1, strLen(fullName));

    // Find the first space character to split the full name into first and middle name
    int firstSpace = strFind(fullName, " ", 0, 1);
    if (firstSpace >= 0 && firstSpace < lastSpace)
    {
        firstName = subStr(fullName, 1, firstSpace - 1);
        middleName = subStr(fullName, firstSpace + 1, lastSpace - firstSpace - 1);
    }
    else
    {
        // No space found before last space, assume the full name is just a first name and last name
        firstName = "";
        middleName = "";
    }
}
else
{
    // No space found, assume the full name is just a first name
    firstName = fullName;
}

this is how the object passes "fullName":"mh ali hasan"

i got lastname ="mh ali hasan" sounds like he doesn't recognize the spaces

FH-Inway
  • 4,432
  • 1
  • 20
  • 37
user4833581
  • 107
  • 1
  • 3
  • 14
  • What is the question? – Jan B. Kjeldsen Mar 23 '23 at 16:46
  • Review the [documentation of the strFind function](https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-string-run-time-functions#remarks-5), particularly the "Remarks" section, which contains an explanation how the negative `_number` parameter works. – FH-Inway Mar 23 '23 at 18:55

1 Answers1

2

Don't reinvent the wheel if you don't have to. Modify as needed to handle special characters or other circumstances.

[firstName, middleName, lastName] = DirPerson::splitNameParts("John Michael Smith");

Alex Kwitny
  • 11,211
  • 2
  • 49
  • 71