{"id":68373,"date":"2005-12-08T14:55:00","date_gmt":"2005-12-08T14:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/08\/how-can-i-convert-names-to-proper-case\/"},"modified":"2005-12-08T14:55:00","modified_gmt":"2005-12-08T14:55:00","slug":"how-can-i-convert-names-to-proper-case","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-convert-names-to-proper-case\/","title":{"rendered":"How Can I Convert Names to Proper Case?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I have a script that my help desk people use to create user accounts. Unfortunately, sometimes these help desk personnel get in a hurry and type in names like this: kEn MYEr. How can I convert names to proper case (i.e., Ken Myer)?<BR><BR>&#8212; LC<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, LC. You know what: you\u2019re in luck. Usually when it comes to doing anything properly the Scripting Guys are the <I>last<\/I> people you\u2019d want to ask. In fact, the only exception that we know of is converting names to proper case, with the first letter in the name written in uppercase and the remaining letters written in lowercase. <I>That<\/I> we can do.<\/P>\n<TABLE class=\"dataTable\" id=\"E6C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Hey, everyone has to be able to do <I>something<\/I>. And while we would have preferred being able to throw a fastball past Albert Pujols or winning an Olympic gold medal, being able to convert names to proper case would have been our next choice anyway.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We\u2019ll warn you in advance that the script that performs this task might look a bit cryptic; that\u2019s because VBScript (unlike Visual Basic) doesn\u2019t have a built-in method for converting strings to proper case. But that\u2019s OK; after all, if it was <I>too<\/I> easy it wouldn\u2019t be any fun:<\/P><PRE class=\"codeSample\">strFirstName = &#8220;kEn&#8221;\nstrLastName = &#8220;MYEr&#8221;<\/p>\n<p>intFirstName = Len(strFirstName)\nstrFirstLetter = UCase(Left(strFirstName, 1))\nstrRemainingLetters = LCase(Right(strFirstName, intFirstName &#8211; 1))<\/p>\n<p>strFirstName = strFirstLetter &amp; strRemainingLetters<\/p>\n<p>intLastName = Len(strLastName)\nstrFirstLetter = UCase(Left(strLastName, 1))\nstrRemainingLetters = LCase(Right(strLastName, intLastName &#8211; 1))<\/p>\n<p>strLastName = strFirstLetter &amp; strRemainingLetters<\/p>\n<p>Wscript.Echo strFirstName, strLastName\n<\/PRE>\n<P>Let\u2019s walk you through the process. To begin with, we simply assign the values <B>kEn<\/B> and <B>MYEr<\/B> to variables named strFirstName and strLastName; these, needless to say, are the two names we need to convert.<\/P>\n<TABLE class=\"dataTable\" id=\"E2D\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Yes, even though it was needless to say, we said it anyway. Go figure.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Seeing as how first things are supposed to come first, we begin by tackling the user\u2019s first name. To do that, we use the <B>Len<\/B> function to determine the number of letters in the string kEn (you got it: there are 3 letter in kEn):<\/P><PRE class=\"codeSample\">intFirstName = Len(strFirstName)\n<\/PRE>\n<P>Next we need to grab <I>just the first letter<\/I> of the name and convert it to uppercase. We do that by combining a pair of functions. We use the <B>Left<\/B> function to take the first letter; that is, we take one letter from the left side of the string (in case you\u2019re wondering, the <B>1<\/B> indicates the number of letters we want to grab):<\/P><PRE class=\"codeSample\">Left(strLastName, 1)\n<\/PRE>\n<P>That\u2019s going to give us the letter k. We then use the <B>UCase<\/B> function to convert that letter to uppercase:<\/P><PRE class=\"codeSample\">UCase(Left(strLastName, 1))\n<\/PRE>\n<P>We now have the uppercase letter K which we store in a variable named strFirstLetter. That\u2019s an awful lot, yet all those steps are carried out in a single line of code:<\/P><PRE class=\"codeSample\">strFirstLetter = UCase(Left(strLastName, 1))\n<\/PRE>\n<P>See how that works? OK. Now we need to convert all the remaining letters in the name to lowercase. That\u2019s what we do with this line of code:<\/P><PRE class=\"codeSample\">strRemainingLetters = LCase(Right(strLastName, intLastName &#8211; 1))\n<\/PRE>\n<P>Yes, it <I>does<\/I> look a little crazy. So let\u2019s break it down. What we want to do is take all the letters in the first name <I>except<\/I> for the very first letter. To do that we use the <B>Right<\/B> function and, beginning from the right, take <I>x<\/I> number of letters. What is <I>x<\/I>? Well, in this case <I>x<\/I> will be the total number of letters in the string minus 1. In other words, 3 &#8211; 1, or 2. That\u2019s going to give us the letters En (which is all we want) leaving out the initial letter k.<\/P>\n<P>Make sense? Here\u2019s the code that does that:<\/P><PRE class=\"codeSample\">Right(strLastName, intLastName &#8211; 1)\n<\/PRE>\n<P>And what do we <I>do<\/I> with those letters? Well, this time we use the <B>LCase <\/B>function to convert each letter to lowercase:<\/P><PRE class=\"codeSample\">LCase(Right(strLastName, intLastName &#8211; 1))\n<\/PRE>\n<P>After that we take those lowercase letters and stash them in a variable named strRemainingLetters:<\/P><PRE class=\"codeSample\">strRemainingLetters = LCase(Right(strLastName, intLastName &#8211; 1))\n<\/PRE>\n<P>Yes, it can be a little confusing. But walk yourself through the code a time or two and you should catch on. Alternatively, modify the code and do the Left\/Right thing first and <I>then<\/I> call UCase or LCase:<\/P><PRE class=\"codeSample\">intFirstName = Len(strFirstName)<\/p>\n<p>strFirstLetter = Left(strFirstName, 1)\nstrFirstLetter = UCase(strFirstLetter)<\/p>\n<p>strRemainingLetters = Right(strFirstName, intFirstName &#8211; 1)\nstrRemainingLetters = LCase(strRemainingLetters)\n<\/PRE>\n<P>If it helps to do things step-by-step, well, so much the better.<\/P>\n<P>Finally, we need to reconstruct the user\u2019s first name. To do that we use the variable strFirstLetter (which contains the uppercase version of the first letter in the user\u2019s name) and combine it with the variable strRemainingLetters (which contains the lowercase version of all the remaining letters in the user\u2019s name):<\/P><PRE class=\"codeSample\">strFirstName = strFirstLetter &amp; strRemainingLetters\n<\/PRE>\n<P>We repeat this entire process for the last name, then echo back the \u201cnew\u201d first and last names for our user:<\/P><PRE class=\"codeSample\">Wscript.Echo strFirstName, strLastName\n<\/PRE>\n<P>And what do we get when we do this?<\/P><PRE class=\"codeSample\">Ken Myer\n<\/PRE>\n<P>It\u2019s a thing of beauty. And properly done, too!<\/P><BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a script that my help desk people use to create user accounts. Unfortunately, sometimes these help desk personnel get in a hurry and type in names like this: kEn MYEr. How can I convert names to proper case (i.e., Ken Myer)?&#8212; LC Hey, LC. You know what: you\u2019re in luck. [&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":[3,4,21,5],"class_list":["post-68373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a script that my help desk people use to create user accounts. Unfortunately, sometimes these help desk personnel get in a hurry and type in names like this: kEn MYEr. How can I convert names to proper case (i.e., Ken Myer)?&#8212; LC Hey, LC. You know what: you\u2019re in luck. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68373","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=68373"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68373\/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=68373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}