TIL: ucFirst() for Word Casing in Lucee CFML

Despite 10+ years of CFML programming, I’m still stumbling across new functions. Today, it was the awkwardly named but surprisingly handy ucFirst, a Lucee-specific function for dealing with string capitalization.

I’ll just quote the docs, regarding what ucFirst does:

Transforms the first letter of a string to uppercase or the first letter of each word, and optionally lowercase uppercase characters.

Admittedly, that’s a bit of a mishmash, with three different modes of functionality:

Now, I’ll be this first to admit that this is not mind-blowing stuff - but it’s just what I was looking for.

I was trying to correct user-provided names that were, in many cases (pun-intended), improperly cased; e.g. BRENDA FLOR, rafael Kerr, and other variations. A little Googling to find a solution showed that this isn’t an uncommon problem:

As you can see, there are a range of solutions, with varying degrees of control, complexity, and accuracy. Your mileage will vary, depending on the ColdFusion engine that you’re using and your application’s requirements (Don’t forget to account for Celtic names!)

If you’re using Lucee, nothing beats ucFirst for simplicity, as it’s built in. And while there’s no guarantee that its output is 100% correct, in our particular use case, it was accurate enough:

newFirstName = ucFirst( firstName, true, true );
newLastName = ucFirst( lastName, true, true );

So, there you go - another tool that might come in handy, should the need arise.