Ask:
Hello, Scripting Guy! How do I truncate a name so that it contains a maximum of 16 characters?
--BN
For:
Hello, BN. Get up: We're going to start remembering the promenade again. When a Scripting Guy went to college, he found a summer job at the Green Giant, tasked with overseeing the harvesting of asparagus in eastern Washington in his office. At the time, the Hulk had a clunky computer system to record the amount of asparagus-and then to record how much the Reapers were paid. This system has a problem, but (in fact, it's more than one problem, but ...) ): It is set to a maximum of 10 characters, while many workers have a last name longer than 10 characters (for example, Myer-ackerman). Each time a data entry person enters a name that is more than 10 characters in length, the entire system is locked, so their workstation often has to reboot. (This is all before Microsoft Windows.) )
The Scripting Guy wasn't even a Scripting Guy at the time; in fact, his experience with computers was largely limited to playing Zork on Commodore 64. But now, as the only university student in the office, he has been appointed to repair the data entry process (fortunately, the database itself does not have this restriction). His job is to make sure that you truncate any names that are more than 10 characters 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 Guys worked in a proprietary branch of BASIC, and his solution was to use the left function to get the first 10 letters of a name as the worker's last name. Now, almost 20 years later, what's the solution to your problem?
Copy Code code as follows:
StrName = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
StrName = Left (StrName, 16)
WScript.Echo StrName
The classic solution never goes out of style, does it? This script starts where we stopped 20 years ago: It assigns a long string (in this case, the alphabet) to a variable named StrName:
StrName = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Next is the following line of code:
StrName = Left (StrName, 16)
This line of code assigns a new value to the variable strName. So what's the new value? It will be the first 16 characters of the StrName current value. This is the function of the left function: it counts from the first character of the string, counting to 16 characters, like this:
Abcdefghijklmnopqrstuvwxyz
These 16 characters, and only these 16 characters, are assigned to the variable strName. Then, echoing the new value of strName, we get the following string:
Abcdefghijklmnop
It's cool. This script works very well even if strName is starting with a 2,600 character 26 character: The result is only the first 16 characters. Ah, but what if the string is less than 16 characters? For example, does this script fail because it attempts to get 16 characters and does not have 16 characters at all?
StrName = "ABCD"
StrName = Left (StrName, 16)
WScript.Echo StrName
Luckily the answer is no: This script will run smoothly. If a string is less than 16 characters, the left function only gets all the characters it can find and assigns this value to StrName. In this case, StrName equals ABCD, the only 4 characters in this string.
Now, remember the good old days. Did we tell you that when the Scripting Guy was sitting in his green giant's office, a rattlesnake crawled in? It's true. He was sitting there, trying to think of a way to limit his last name to 10 characters, and that was when--oh, my God, it's time for today. We must go on with the story another day.