How Can I Convert Names to Proper Case?


Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script that my help desk people use to create user accounts. Unfortunately, sometimes these help desk personnel get in a hurry and type in names like this: kEn MYEr. How can I convert names to proper case (i.e., Ken Myer)?

— LC

Spacer Hey, Scripting Guy! Answer Script Center

Hey, LC. You know what: you’re in luck. Usually when it comes to doing anything properly the Scripting Guys are the last people you’d want to ask. In fact, the only exception that we know of is converting names to proper case, with the first letter in the name written in uppercase and the remaining letters written in lowercase. That we can do.

Note. Hey, everyone has to be able to do something. And while we would have preferred being able to throw a fastball past Albert Pujols or winning an Olympic gold medal, being able to convert names to proper case would have been our next choice anyway.

We’ll warn you in advance that the script that performs this task might look a bit cryptic; that’s because VBScript (unlike Visual Basic) doesn’t have a built-in method for converting strings to proper case. But that’s OK; after all, if it was too easy it wouldn’t be any fun:

strFirstName = “kEn”
strLastName = “MYEr”

intFirstName = Len(strFirstName)
strFirstLetter = UCase(Left(strFirstName, 1))
strRemainingLetters = LCase(Right(strFirstName, intFirstName – 1))

strFirstName = strFirstLetter & strRemainingLetters

intLastName = Len(strLastName)
strFirstLetter = UCase(Left(strLastName, 1))
strRemainingLetters = LCase(Right(strLastName, intLastName – 1))

strLastName = strFirstLetter & strRemainingLetters

Wscript.Echo strFirstName, strLastName

Let’s walk you through the process. To begin with, we simply assign the values kEn and MYEr to variables named strFirstName and strLastName; these, needless to say, are the two names we need to convert.

Note. Yes, even though it was needless to say, we said it anyway. Go figure.

Seeing as how first things are supposed to come first, we begin by tackling the user’s first name. To do that, we use the Len function to determine the number of letters in the string kEn (you got it: there are 3 letter in kEn):

intFirstName = Len(strFirstName)

Next we need to grab just the first letter of the name and convert it to uppercase. We do that by combining a pair of functions. We use the Left function to take the first letter; that is, we take one letter from the left side of the string (in case you’re wondering, the 1 indicates the number of letters we want to grab):

Left(strLastName, 1)

That’s going to give us the letter k. We then use the UCase function to convert that letter to uppercase:

UCase(Left(strLastName, 1))

We now have the uppercase letter K which we store in a variable named strFirstLetter. That’s an awful lot, yet all those steps are carried out in a single line of code:

strFirstLetter = UCase(Left(strLastName, 1))

See how that works? OK. Now we need to convert all the remaining letters in the name to lowercase. That’s what we do with this line of code:


Yes, it does look a little crazy. So let’s break it down. What we want to do is take all the letters in the first name except for the very first letter. To do that we use the Right function and, beginning from the right, take x number of letters. What is x? Well, in this case x will be the total number of letters in the string minus 1. In other words, 3 – 1, or 2. That’s going to give us the letters En (which is all we want) leaving out the initial letter k.

Make sense? Here’s the code that does that:

Right(strLastName, intLastName – 1)

And what do we do with those letters? Well, this time we use the LCase function to convert each letter to lowercase:

LCase(Right(strLastName, intLastName – 1))

After that we take those lowercase letters and stash them in a variable named strRemainingLetters:


Yes, it can be a little confusing. But walk yourself through the code a time or two and you should catch on. Alternatively, modify the code and do the Left/Right thing first and then call UCase or LCase:

intFirstName = Len(strFirstName)

strFirstLetter = Left(strFirstName, 1)
strFirstLetter = UCase(strFirstLetter)

strRemainingLetters = Right(strFirstName, intFirstName – 1)
strRemainingLetters = LCase(strRemainingLetters)

If it helps to do things step-by-step, well, so much the better.

Finally, we need to reconstruct the user’s first name. To do that we use the variable strFirstLetter (which contains the uppercase version of the first letter in the user’s name) and combine it with the variable strRemainingLetters (which contains the lowercase version of all the remaining letters in the user’s name):

strFirstName = strFirstLetter & strRemainingLetters

We repeat this entire process for the last name, then echo back the “new” first and last names for our user:

Wscript.Echo strFirstName, strLastName

And what do we get when we do this?

Ken Myer

It’s a thing of beauty. And properly done, too!



No Comments.