Skip to main content
Known Participant
November 10, 2009
Question

What CF tags do I use ? - still cannot get it to work

  • November 10, 2009
  • 3 replies
  • 807 views

I think I saw this post somewhere but I cannot find it. I tried myself and got it partially to work but not entirely.

I have a name, like Joe Smith, and I need to output it to J. Smith. I think I can do that. But where I have problem is if the the name is Joe C Smith (no period after the middle C). I need to convert that to J.C. Smith, or J. C. Smith. My current technique makes it J. C Smith (no period after the middle C).

What coldfusion functions would I use to convert Joe C Smith to J.C. Smith, or what sql commands, if that is a better approach.

This topic has been closed for replies.

3 replies

BKBK
Community Expert
Community Expert
November 15, 2009

To turn to New York Guy's original question, you could put the required functionality in a function, like this:

<cfscript>
function convertToInitials(fullName) {
    // array to store name initials
    var initial = arrayNew(1);
    var convertedName = "";
    var currentName = "";
    var isCurrentNameInitial = false;
    var surname = listlast(fullname," ");
    var numberOfNameElements = listLen(fullName," ");
    // initialize array
    var arrayIsSet = arraySet(initial, 1,numberOfNameElements, "");
    var i = 1;
    if (numberOfNameElements GT 1) {
        for (i=1; i LT numberOfNameElements; i=i+1) {
            // i-th name
            currentName = listGetAt(fullName,i," ");
            // Test whether current name already an initial of the form A., A.B., A.B.C., etc?
            isCurrentNameInitial = REfindNoCase("([a-z]\.)+$",currentName,1,false) GT 0;           
            if (isCurrentNameInitial) {
                initial = currentName;
                convertedName = convertedName & initial;
            }
            else {
                // get uppercased initial letter of i-th name
                initial = ucase(left(currentName,1));
                convertedName = convertedName & initial & ".";
            }
        }
    }
    convertedName = convertedName & " " & surname;
    return convertedName;
}
</cfscript>

<!--- Test the func! --->

The New York Guy: <cfoutput>#convertToInitials("The New York Guy")#</cfoutput><br>

A.K.A. The New York Guy: <cfoutput>#convertToInitials("A.K.A. The New York Guy")#</cfoutput>

Inspiring
November 17, 2009

As per Ian's earlier post, that sort of function's not much chop if your name is Leonardo da Vinci or Vincent van Gogh or similar.

--

Adam

BKBK
Community Expert
Community Expert
November 17, 2009
As per Ian's earlier post, that sort of function's not much chop if your name is Leonardo da Vinci or Vincent van Gogh or similar.

Per Ian's post, it actually is! Look at his M.J.S.V.D. Longname. Hence, Signor L.D. Vinci and De Heer V.V. Gogh. It was actually my earlier post that pointed out that van de LangeNaam  is a single name in The Netherlands. Hence, V. van Gogh, not V.V. Gogh.

However, we're in the English-speaking world. The function is an answer to New York Guy's original question.

ilssac
Inspiring
November 10, 2009

You would probably have some luck using the list functions and treating your name as a space delimited list.

http://livedocs.adobe.com/coldfusion/8/htmldocs/functions-pt0_13.html

<cfouput>#listLen("Joe C Smith"," ")#</cfoutput>

But I do wonder how you plan to account for my favorite name "Mary Joe Sue Von der LongName".  An unusual name from a mix of cultures, but it does demonstrate that all names are not three single words for first, middle and last.

BKBK
Community Expert
Community Expert
November 12, 2009
I do wonder how you plan to account for my favorite name "Mary Joe Sue Von der LongName".

We in The Netherlands would say M.J.S. van de LangeNaam, or just plain Mary Joe. The surname isn't just LangeNaam, but van de LangeNaam.

ilssac
Inspiring
November 12, 2009

BKBK wrote:

I do wonder how you plan to account for my favorite name "Mary Joe Sue Von der LongName".

We in The Netherlands would say M.J.S. van de LangeNaam, or just plain Mary Joe. The surname isn't just LangeNaam, but van de LangeNaam.

Exactly why I love this example.  There may not be many southern U.S. expatriates living in The Netherlands and|or Dutch emigrants to the U.S,. south that have chosen to mix name traditions in such a manner.  But with 6+ Billion people in the world, there maybe at least one or two such people.

And if her name is in ones databases I just hope she is not upset when ones system names her M.J.S.V.D. Longname.

Inspiring
November 10, 2009

You could use list functions to remove the last element of a space-delimited list to be the surname, and then treat the remainder as a list of forenames loop over them, truncating them.

You could probably use a regex replace to do it also, but I haven't had coffee yet so am not going to tackle that one.

One note though.  What about names that don't follow the pattern "forename[ forename[...]] surname", like a lot of non-european names do?  Some name patterns have the family name first, and the given names second.

To be honest, I would not monkey with people's names, I'd just leave them as the way they chose to type them in.

--

Adam