Ever wonder why there are built in functions to make stuff lower case and
upper case, but not one to put something into proper case? Well we did! It's
built into Visual Basic using the StrConv(string, vbProperCase) command so why
can't we use it in VBScript? Well now you can.
I've run into this quite a few times and for the most part it's not a big
problem. Each time I need to do it I usually use a quick hack like:
to handle it, but I've probably written it a dozen times and it gets annoying.
Finally, one of our visitors got tired of doing this over and over,
decided that enough is enough, and spent the time to write a function
to help automate this task. Luckily for you, Brian Shamblen from
Certified Capital Corporation
is not only the type of person who tries to make
his own coding easier, he also likes to share! I received a version of this
function from Brian who was visiting the site and felt that he'd like to
contribute it in the hopes that others would benefit from his work. I've
"hacked and slashed" it a little and added some comments to make
it seem more like an official ASP 101 sample, but the motivation and real
credit goes to someone probably very much like you! You see... we actually
do listen to our users once in a while! Thanks Brian!
When I recieved it, it was actually named TCase for title case. I've changed
it to PCase for proper case to match it to the term that Visual Basic uses for
this type of string. There are actually a few issues with the script. The
main issue is: what to do with things you want to keep capitalized or those you
don't? For example, I'd assume you'd want McCarthy to stay McCarthy and not
change to Mccarthy. Well it doesn't. Similarly "a", "an",
and "the" get changed to "A", "An", and
"The" even though you wouldn't want this in a title. As far as I
know, there's not easy way to handle this type of thing in a dozen line
script, so we simply don't! I replicates the functionality of VB's
StrConv(string, vbProperCase). If you want any more than that, I'm sorry but
you'll need to look elsewhere.
Alternate Version of PCase
One of our visitors sent in an alternate version of the VBScript PCase
function we published in this sample. While the original version accomplished
its task using all string-based functions, his version splits the phrase into
an array of words and then loops through the array capitalizing each word.
It just goes to show that no matter how simple a problem is, there's almost
always more than one solution.
Rather then list the code here or create a zip file, I've simply added a new
function named "PCaseAlternate" to the original script. So if
you're interested in the alternate version of the code just take another look at the
ASP source code.