Use VBS to convert names to the correct case code _vbs

Source: Internet
Author: User
Tags lowercase
Ask:
Hello, Scripting Guy! I have a script that my help support staff uses to create user accounts. Unfortunately, sometimes these help support people would type a name like this in a fuss: KEn MYEr. How do I convert a name to the correct case (that is, Ken Myer)?
--LC
For:
Hello, LC. You know, you're lucky. When it comes to doing things right, the Scripting Guys are usually the last people you want to find. In fact, as we know, there is only one exception, which is to convert the name to the correct case, that is, to capitalize the first letter in the name, and the remaining letters to lowercase. We can still do that.
Note: Hey, everyone has to be able to do something. Although we would prefer to be able to cast more than Albert Pujols or win an Olympic gold medal, the ability to convert the name to the correct case will be our next choice anyway.
We warn you beforehand that the script that performs this task may look a bit cryptic, because VBScript (unlike Visual Basic) does not have built-in methods for converting strings to the correct case. But it doesn't matter, after all, if things are too easy, there's no fun at all:
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 introduce you to the process step-by-step. In the beginning, we just assigned the value KEn and MYEr to the variables named strFirstName and strLastName; Needless to say, this is the two names we need to convert.
Note: Yes, although needless to say, we did. Go ahead and think for yourself.
Let's take a look at how to start first, we started with the user's name. To do this, we use the Len function to determine the number of letters in the string Ken (right: Ken has three letters):
Intfirstname = Len (strfirstname)
Next, we just need to get the first letter of the name and convert it to uppercase. This is accomplished by a combination of functions. We use the left function to get the first letter, that is, to get a letter from the left-hand side of the string (if you're curious about it, then 1 means the number of letters we're going to get):
Left (strLastName, 1)
So we'll get the letter K. We then use the UCase function to convert the letter to uppercase:
UCase (Left (strLastName, 1))
Now that we have the capital letter K, we store it in a variable named strFirstLetter. This is too verbose, but all of these steps are executed in one line of code:
strFirstLetter = UCase (Left (strLastName, 1))
Do you understand how this works? Good. Now, we need to convert all the remaining letters in the name to lowercase. This is what we do with the following line of code:
strRemainingLetters = LCase (Right (strLastName, intLastName-1))
Yes, it does look a little crazy. So let's take it apart and look at it. All we have to do is get all the letters in the name except the first letter. To do this, we use the right function to get the X-letter from the right-hand start. What is X? Well, in this case, X will be the total number of letters in the string minus 1. In other words, 3 minus 1, or 2. So we'll get the letter En (that's all we need), and skip the start letter K.
Do you understand? Here's the implementation code:
Right (strLastName, intLastName-1)
So, what do we do with those letters? Well, this time we're going to use the LCase function to convert each letter to lowercase:
LCase (Right (strLastName, intLastName-1))
After that, we get these lowercase letters and store them in a variable named strremainingletters:
strRemainingLetters = LCase (Right (strLastName, intLastName-1))
Yes, it may be a little confusing. But as long as you have the code one or two times in person, you should be able to figure it out. Alternatively, you can modify the code to perform the Left/right section before calling UCase or LCase:
Intfirstname = Len (strfirstname)
strFirstLetter = Left (strFirstName, 1)
strFirstLetter = UCase (strfirstletter)
strRemainingLetters = Right (strFirstName, intFirstName-1)
strRemainingLetters = LCase (strremainingletters)
It would be great if you were helped by the step-by-step.
Finally, we need to reconstruct the user's name. To do this, we use variable strfirstletter, which contains the uppercase version of the first letter in the user's name, and merge it with the variable strremainingletters, which contains the lowercase versions of all the remaining letters in the user's name:
strFirstName = strFirstLetter & strremainingletters
Repeat the entire process for the last name, and then echo the "new" user name.
WScript.Echo strFirstName, strLastName
Let's see what we get out of this.
Ken Myer
It's really beautiful. And it's done properly!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.