{"id":67903,"date":"2006-02-22T14:16:00","date_gmt":"2006-02-22T14:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/22\/how-can-i-truncate-names-to-a-maximum-of-16-characters\/"},"modified":"2006-02-22T14:16:00","modified_gmt":"2006-02-22T14:16:00","slug":"how-can-i-truncate-names-to-a-maximum-of-16-characters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-truncate-names-to-a-maximum-of-16-characters\/","title":{"rendered":"How Can I Truncate Names to a Maximum of 16 Characters?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I truncate names to a maximum of 16 characters?<BR><BR>&#8212; BN<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, BN. Brace yourself: it\u2019s 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 &#8211; and thus the amount of money owed &#8211; to all the harvesters. This system had one problem, however (well, actually, it had more than <I>one<\/I> problem, but \u2026): 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.)<\/P>\n<P>Now, this Scripting Guy was anything <I>but<\/I> a Scripting Guy back then; in fact, his computer experience was largely limited to playing <I>Zork<\/I> 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.<\/P>\n<P>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 <B>Left<\/B> function to take just the first 10 letters of a name and call that the worker\u2019s last name. And now, 20-some years later, guess what the solution is to <I>your<\/I> problem?<\/P><PRE class=\"codeSample\">strName = &#8220;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#8221;\nstrName = Left(strName, 16)\nWscript.Echo strName\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">strName = &#8220;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#8221;\n<\/PRE>\n<P>That brings us to this line of code:<\/P><PRE class=\"codeSample\">strName = Left(strName, 16)\n<\/PRE>\n<P>With this line of code we\u2019re assigning a new value to the variable strName. And what <I>is<\/I> that new value? It\u2019s going to be the first 16 characters of the existing value of strName. That\u2019s the purpose of the Left function: it starts with the first character in the string and counts out the next 16 characters, like so:<\/P>\n<P><B>ABCDEFGHIJKLMNOP<\/B>QRSTUVWXYZ<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">ABCDEFGHIJKLMNOP\n<\/PRE>\n<P>Cool. That works great, and it doesn\u2019t 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 <I>less<\/I> than 16 character? For example, is this script going to fail, because it tries to grab 16 characters and there <I>aren\u2019t<\/I> 16 characters?<\/P><PRE class=\"codeSample\">strName = &#8220;ABCD&#8221;\nstrName = Left(strName, 16)\nWscript.Echo strName\n<\/PRE>\n<P>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.<\/P>\n<P>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\u2019s true. He was sitting there, trying to figure out how to limit last names to 10 characters when &#8212; oops; we\u2019re out of time for today. We\u2019ll have to get back to that story some other time.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I truncate names to a maximum of 16 characters?&#8212; BN Hey, BN. Brace yourself: it\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[174,3,4,21,5],"class_list":["post-67903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-regular-expressions","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I truncate names to a maximum of 16 characters?&#8212; BN Hey, BN. Brace yourself: it\u2019s 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67903\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}