Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
^_^
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
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>
Copy link to clipboard
Copied
Part of the problem is I was writing in CFSCRIPT and you're trying tagform.
<CFSET
fullname = "Dick Van George Jettson">
<CFSET thisLen = listLen(fullname," ")>
<cfif thisLen eq 2>
<cfset concat = left(ListGetAt(fullname,1," ",true),1) & left(ListGetAt(fullname,2," ",true),5)>
<cfelse>
<cfset concat = left(ListGetAt(fullname,1," ",true),1)>
<cfset lastname = "">
<cfloop index="idx" from="2" to="#thisLen#">
<cfset lastname &= ListGetAt(fullname,i," ",true)>
</cfloop>
<cfset concat &= left(lastname,5)>
</cfif>
<cfoutput>#concat#</cfoutput>
I hate this editor..
^_^