How Can I Truncate Names to a Maximum of 16 Characters?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I truncate names to a maximum of 16 characters?

— BN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BN. Brace yourself: it’s time for another trip down memory lane. When one of the Scripting Guys was going to college, he took a temporary summer job with the Green Giant Corporation, working in the office that oversaw the asparagus harvest in eastern Washington. At that time Green Giant had a clunky old computer system that was used to record the amount of asparagus – and thus the amount of money owed – to all the harvesters. This system had one problem, however (well, actually, it had more than one problem, but …): it had been set up to accept a maximum of 10 characters as a last name, and many of the workers had last names longer than that (e.g., Myer-Ackerman). Each time a data entry person entered a name longer than 10 characters the entire system would lock up, and their workstation would often have to be rebooted. (This wall all in the days before Microsoft Windows.)

Now, this Scripting Guy was anything but a Scripting Guy back then; in fact, his computer experience was largely limited to playing Zork on a Commodore 64. Nevertheless, as the lone college boy in the office he was given the task of trying to fix the data entry program (the database itself was off limits, and for good reason). His job was to make sure that any name longer than 10 characters was truncated before the program tried to save that name to the database.

In other words, something very similar to what you need to do. Back then our Scripting Guy was working in some proprietary offshoot of BASIC, but his solution was to use the Left function to take just the first 10 letters of a name and call that the worker’s last name. And now, 20-some years later, guess what the solution is to your problem?

strName = Left(strName, 16)
Wscript.Echo strName

The classic solutions never go out of style, do they? This script picks up where we left off 20 years ago: it assigns a big long string (in this case, the alphabet) to a variable named strName:


That brings us to this line of code:

strName = Left(strName, 16)

With this line of code we’re assigning a new value to the variable strName. And what is that new value? It’s going to be the first 16 characters of the existing value of strName. That’s the purpose of the Left function: it starts with the first character in the string and counts out the next 16 characters, like so:


Those 16 characters, and only those 16 characters, get assigned to the variable strName. We then echo back the new value of strName, and we get this:


Cool. That works great, and it doesn’t matter whether strName starts off with 26 characters of 2600 characters: in the end, only the first 16 characters will get used. Ah, but what happens if a string has less than 16 character? For example, is this script going to fail, because it tries to grab 16 characters and there aren’t 16 characters?

strName = “ABCD”
strName = Left(strName, 16)
Wscript.Echo strName

Fortunately the answer is no: this script works just fine. If a string has less than 16 characters the Left function simply takes whatever characters it can find and then assigns that value to strName. In this case, strName will be equal to ABCD, the only 4 characters in the string.

Now, back to reminiscing about the good old days. Did we ever tell you about the time this Scripting Guy was sitting in his office at Green Giant when a rattlesnake slithered in? It’s true. He was sitting there, trying to figure out how to limit last names to 10 characters when — oops; we’re out of time for today. We’ll have to get back to that story some other time.


Discussion is closed.

Feedback usabilla icon