Skip to main content
Inspiring
April 24, 2013
Question

String Functions to extract first name initial and first 5 letters of last name

  • April 24, 2013
  • 1 reply
  • 2566 views

I am trying to extract the first letter of a firstname and the first 5 letters off a last name and concatate the two values

For example I have a data field with "John Smith" in the field.

I want to get the first letter of the first name -  "John" = J

I want to get the first 5 letters of the last name- "Smith" = smith

and then concatate the 2 values = jsmith

another example

For example I have a data field with "Reese Witherspoon" in the field.

I want to get the first letter of the first name -  "Reese" = R

I want to get the first 5 letters of the last name- "Witherspoon" = withe

and then concatate the 2 values = rwithe

How would I do that ?

I think I know how to do each step individually but is there a better way to get the value?

    This topic has been closed for replies.

    1 reply

    WolfShade
    Legend
    April 24, 2013

    As long as first and last name are being submitted separately, it's simple.

    concat = left(firstname,1) & left(lastname,5)

    Pseudo-code, of course.

    ^_^

    WolfShade
    Legend
    April 24, 2013

    Actually, now that I think about it.. even if first and last name are in the same field or variable, as long as middle name/initial are not being used, you can still do it, simply.

    fullname = "Reese Witherspoon";

    concat = left(ListGetAt(fullname,1," ",true),1) & left(ListGetAt(fullname,2," ",true),5);

    ^_^

    UPDATE: Minor modification.  This works as long as the last name isn't something like "Van Dyke" or "Van Zandt".  SOOOOOOOO, check the list length (with " " as delimiter) and if it's greater than two, join positions 2 through x together, removing all spaces.

    fullname = "Dick Van George Jettson"; // It didn't like D y k e.. sigh..

    thisLen = listLen(fullname," "); // 4
     

    if (thisLen eq 2){

       concat = left(ListGetAt(fullname,1," ",true),1) & left(ListGetAt(fullname,2," ",true),5);

    }

    else {

       concat = left(ListGetAt(fullname,1," ",true),1);

       lastname = "";

       for(i = 2; i lte thisLen; i=(i+1)){

          lastname &= ListGetAt(fullname,i," ",true);

          }

        concat &= left(lastname,5);

    }

    weezerboyAuthor
    Inspiring
    April 24, 2013

    This look like it works but was is the code exactly? I cant seem to get everything in the correct place

    <CFSET fullname = "Dick Van George Jettson">; // It didn't like D y k e.. sigh..

    <CFSET thisLen = listLen(fullname," ")
    >; // 4
     
    <cfif (thisLen eq 2)>{
       concat = left(ListGetAt(fullname,1," ",true),1) & left(ListGetAt(fullname,2," ",true),5);
    }
    <cfelse> {
       concat = left(ListGetAt(fullname,1," ",true),1);
       lastname = "";
       for(i = 2; i lte thisLen; i=(i+1)){
          lastname &= ListGetAt(fullname,i," ",true);
          }
        concat &= left(lastname,5);
    }

    </cfif>