Use vbs to truncate a name so that it can contain up to 16 characters

Source: Internet
Author: User

Q:
Hello, script expert! How can I truncation a name so that it can contain a maximum of 16 characters?
-- BN
A:
Hello, bn. Fighting Spirit: we have to start our journey to remember the corridor. When a script expert went to college, he found a summer temporary job at the Hulk company to supervise asparagus harvesting in East Washington in the office. At that time, hulk had an old and bulky computer system to record the number of asparagus-and then record how much it would pay to all the cuts. This system has a problem, but (in fact, it has more than one problem, ...) : It can be set to a last name with a maximum of 10 characters, while many workers have a longer surname than 10 characters (for example, Myer-Ackerman ). Each time a data input person enters a name longer than 10 characters, the entire system will be locked, so their workstation often has to be restarted. (This is all before Microsoft Windows .)
This scripting expert was not a scripting expert at that time; in fact, his computer experience was limited to playing zork on Commodore 64. But now, as the only college student in the office, he is appointed to repair the data input. Program (Fortunately, the database itself does not have such restrictions ). His job is to ensure that all names with more than 10 characters are truncated before the program tries to save the name to the database.
In other words, this is very similar to the task you need to complete. At that time, our scripting expert was working in a proprietary branch of basic. His solution was to use the left function to get the first 10 letters of a name and use it as the worker's surname. Now, about 20 years later, what is the solution to your problem? CopyCode The Code is as follows: strname = "abcdefghijklmnopqrstuvwxyz"
Strname = left (strname, 16)
Wscript. Echo strname

classic solutions are never outdated, right? This script starts from where we stopped 20 years ago: it assigned a long string (the alphabet in this example) to a variable named strname:
strname = "abcdefghijklmnopqrstuvwxyz"
the following code is used:
strname = left (strname, 16)
This line of code assigns a new value to the variable strname. So what is the new value? It will be the first 16 characters of the current strname value. This is the role of the Left function: It starts counting from the first character of the string and continues to count to 16 characters, just like this:
abcdefghijklmnopqrstuvwxyz
these 16 characters, only these 16 characters are assigned to the variable strname. Then, echo the new strname value and we will get the following string:
abcdefghijklmnop
it's cool. This script is very effective, even if the strname starts from 26 characters of 2600 characters, the effect is the same: the results only use the first 16 characters. Ah, but what if the string is less than 16 characters? For example, will this script fail because it tries to get 16 characters without 16 characters at all?
strname = "ABCD"
strname = left (strname, 16)
wscript. echo strname
fortunately, the answer is no: This script runs smoothly. If a string contains less than 16 characters, the left function obtains only all the characters it can find and assigns this value to strname. In this example, strname is equal to ABCD, which contains only four characters.
now, remember the good times of the past. We have told you that when this scripting expert is sitting in his Hulk's office, is a snake crawling in? This is true. He is sitting there, trying to think about the method of limiting his surname to 10 characters. It's now -- oh, my God; today's time is up. We must continue to tell this story another day.

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.