Swap First and Last Names

This function receives a person’s name and inverts the first and last names.
It will insert a comma after the last name if it is reverted.
Example: “John C. Doe” becomes “Doe, John C.” and “Doe, Jane K.” becomes “Jane K. Doe”
Argument: sName (required), name to be converted

<cfscript>
/*
rpSwapNames()
Author: Ricardo Parente
Date: Oct 16 2000
*/
function rpSwapNames(sName){
var firstName = "";
var lastName = "";
var delim = " ";
var result = arguments.sName;
if (listLen(arguments.sName, delim) lt 2) return result;
if (find(",", arguments.sName)) {
delim = ",";
arguments.reverted = true;
}
firstName = listLast(arguments.sName, delim);
lastName = reverse(listrest(reverse(arguments.sName), delim));
return firstName & iif(arguments.reverted, de(""), de(",")) & " " & lastName;
}
</cfscript>

Bookmark and Share
Jul 25th, 2008 | Posted in ColdFusion
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>