Use vbs to convert the name to correct case-sensitive code

Source: Internet
Author: User

Q:
Hello, script expert! I have a script that my support staff can use to create user accounts. Unfortunately, sometimes these support staff enter a name similar to this: kEn MYEr. How can I convert the name to a correct case (that is, Ken Myer?
-- LC
A:
Hello, LC. You know: You're lucky. When it comes to correct actions, script experts are usually the least people you want to look. In fact, as far as we know, there is only one case, that is, to convert the name to the correct case, that is, to make the first letter in the name in upper case, and make other letters in lower case. We can still do this.
Note: Hey, everyone has to do something. Although we want to play a ball that exceeds Albert Pujols or win the Olympic gold medal, converting the name to the correct case will be our next choice.
We remind you in advance that the script for executing this task may seem a bit confidential; this is because VBScript (unlike Visual Basic) does not have a built-in method for converting strings into correct case. But it doesn't matter. After all, if things are too easy, there will be no 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 gradually introduce the process to you. In the beginning, we only assigned the value kEn and MYEr to the variables strFirstName and strLastName. Needless to say, this is the two names that need to be converted.
Note: Yes, though not to mention, we have to say so. Think about it yourself.
First, let's take a look at how to get started. We started with processing the user name. To this end, we use the Len function to determine the number of letters in the string kEn (by the way, there are three letters in the kEn ):
IntFirstName = Len (strFirstName)
Next, we only need to get the first letter of the name and convert it to uppercase. This is achieved through a combination of functions. We use the Left function to get the first letter, that is, to get a letter from the Left side of the string (if you are curious about this, 1 indicates the number of letters we want to get ):
Left (strLastName, 1)
In this way, we will get the letter k. Then, we use the UCase function to convert the letter to uppercase:
UCase (Left (strLastName, 1 ))
Now we have the capital letter K, and we store it in a variable named strFirstLetter. This is too long, but all these steps are executed through a line of code:
StrFirstLetter = UCase (Left (strLastName, 1 ))
Do you understand how it works? Okay. Now, we need to convert all the other letters in the name to lowercase letters. This is what we do with the following line of code:
StrRemainingLetters = LCase (Right (strLastName, intLastName-1 ))
Yes, it does seem crazy. Let's take it apart. All we need to do is get all the letters except the first letter in the name. Therefore, we use the Right function to obtain x letters from the Right. What is x? In this example, x is the total number of letters in the string minus 1. In other words, it is 3 minus 1, that is, 2. In this way, we will get the letter En (this is what we want), instead of the starting letter k.
Do you understand? The following is the implementation code:
Right (strLastName, intLastName-1)
So what do we do with those letters? Well, this time we will use the LCase function to convert every letter to lower case:
LCase (Right (strLastName, intLastName-1 ))
Then we get these lowercase letters and store them in a variable named strRemainingLetters:
StrRemainingLetters = LCase (Right (strLastName, intLastName-1 ))
Yes, it may be a bit confusing. However, you should understand the code once or twice. Alternatively, you can modify the code by executing Left/Right and then calling UCase or LCase:
IntFirstName = Len (strFirstName)
StrFirstLetter = Left (strFirstName, 1)
StrFirstLetter = UCase (strFirstLetter)
StrRemainingLetters = Right (strFirstName, intFirstName-1)
StrRemainingLetters = LCase (strRemainingLetters)
If the step-by-step operation is helpful to you, it would be better.
Finally, we need to reconstruct the user name. To this end, we use the variable strFirstLetter (which contains the upper-case version of the first letter in the username field) and merge it with the variable strRemainingLetters (which contains the lower-case versions of all the other letters in the username field:
StrFirstName = strFirstLetter & strRemainingLetters
Repeat the above process for the last name, and then display the "new" user name.
Wscript. Echo strFirstName, strLastName
Let's see what we will get by doing this?
Ken Myer
Pretty. It is also done as appropriate!

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.