{"id":71423,"date":"2004-09-16T11:38:00","date_gmt":"2004-09-16T11:38:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/09\/16\/how-do-i-get-rid-of-extraneous-spaces-in-a-string\/"},"modified":"2004-09-16T11:38:00","modified_gmt":"2004-09-16T11:38:00","slug":"how-do-i-get-rid-of-extraneous-spaces-in-a-string","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-do-i-get-rid-of-extraneous-spaces-in-a-string\/","title":{"rendered":"How Do I Get Rid of Extraneous Spaces in a String?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>I know that I can use the Split command to separate a sentence like this &#8211; <i>VBScript is fun!<\/i> &#8211; into an array of individual words. However, what happens if I have a sentence like this: <i>VBScript is fun! <\/i>I can\u2019t use a blank space as the delimiter because I might have lots of blank spaces. And I can\u2019t use a specific number of blank spaces as the delimiter, because the number might vary. Any suggestions?<\/p>\n<p>&#8212; SC<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, SC. You might find this difficult to believe, but we Scripting Guys actually have standards, at least when it comes to this column. We get scores of questions each day, and we can\u2019t respond to each one. So how do we decide which questions get published? Well, first we look for questions that seem reasonably easy to do. Here\u2019s a hint: if your question includes lots of <i>ands<\/i> &#8211; \u201c<i>And<\/i> I want the script to do this <i>and<\/i> I want the script to do that <i>and<\/i> &#8230;.\u201d &#8211; well, then there\u2019s much less chance it will find its way to the top of the pile. We realize that that\u2019s a bit unfair, but for better or worse this column is a very small part of our daily responsibilities. Thus we can\u2019t spend too much time on any one question.<\/p>\n<p>The second thing we look for is mass appeal: will the answer to this question be of interest and of use to most of our audience. Again, that\u2019s not always fair, but questions dealing with \u201cmainstream\u201d technologies will usually win out over more esoteric questions. Sorry.<\/p>\n<p>To tell you the truth, though, we\u2019re not sure if your script fits either category; we decided to answer it simply because <i>we<\/i> found it interesting. After all, how the heck <i>do<\/i> you get rid of all those extraneous spaces, and thus turn <i>VBScript is fun! <\/i>into <i>VBScript is fun!<\/i> ? Here\u2019s the solution we came up with:<\/p>\n<p>The problem here is not that we have spaces in the string; if that was the case, we could just use VBScript\u2019s Replace function to get rid of all the spaces. However, we want a <i>few<\/i> spaces in the string; in fact, we want to separate each word with a space. We just want to get rid of all the <i>extra<\/i> spaces. In the end, we decided to go ahead and use the Replace function, replacing all multi-space instances with a single space. Thus if we found 7 consecutive blank spaces, we\u2019d replace those with a single space.<\/p>\n<p>Pretty simple, except for one problem. The Replace function needs a specific string to search for; you can\u2019t just tell it, \u201cOK, search for multi-space instances and replace them with a single space.\u201d Instead, you have to use code similar to this, which replaces seven blank spaces with one space:<\/p>\n<pre class=\"codeSample\">strStarter = Replace(strStarter, \u201c       \u201c, \" \")\n<\/pre>\n<p>That\u2019s great, except that we don\u2019t know how many blank spaces to search for; on top of that, while we might have 7 consecutive blank spaces in the string, we might also have 5 consecutive blank spaces in that same string. How do we deal with that?<\/p>\n<p>We dealt with it using this script. Here\u2019s the code, which we\u2019ll explain in a second:<\/p>\n<pre class=\"codeSample\">strStarter = \"VBScript                     is                  fun!\"\nintStarter = Len(strStarter)\nFor i = intStarter to 2 Step -1\n    strChars = Space(i)\n    strStarter = Replace(strStarter, strChars, \" \")\nNext\narrStarter = Split(strStarter, \" \")\nFor Each strUnit in arrStarter\n    Wscript.Echo strUnit\nNext\n<\/pre>\n<p>What we ended up doing was saying, \u201cOK, we have to search for strings consisting of blank spaces, but we don\u2019t know how many blank spaces might be in one of these strings.\u201d That was a bit of a stumbling block until we realized, hey, let\u2019s say the string has a total of 37 characters in it. That means the <i>most<\/i> consecutive blank spaces we could find would be 37 (assuming the string was nothing but blank spaces). Therefore, we can start by searching for 37 consecutive blank spaces; if we find them, we replace them with a single blank space. We then search for 36 consecutive blank spaces, then 35, then 34. We continue working our way down until we search for 2 blank spaces, and replace those with a single space. At that point, we should have eliminated all the extraneous blank spaces.<\/p>\n<p>Pretty slick, huh? And, surprisingly enough, it\u2019s easy to do. Notice in our script we start by assigning a string to the variable strStarter. We then use this code to determine how many characters are in strStarter:<\/p>\n<pre class=\"codeSample\">intStarter = Len(strStarter)\n<\/pre>\n<p>Again, let\u2019s say there are 37 characters. What we need now is a loop that will start at 37 and work its way down to 2. And guess what? That\u2019s <i>exactly<\/i> what this loop does:<\/p>\n<pre class=\"codeSample\">For i = intStarter to 2 Step -1\n    strChars = Space(i)\n    strStarter = Replace(strStarter, strChars, \" \")\nNext\n<\/pre>\n<p>The loop starts at the number of characters in the string, then uses the Step -1 parameter to work its way down to 2. What\u2019s Step -1? Well, by default, a For Next loop works its way up one number at a time. For example, in this loop i starts off at 1, and keeps incrementing by 1 until it gets to 10:<\/p>\n<pre class=\"codeSample\">For i = 1 to 10\n<\/pre>\n<p>In our loop, we\u2019re starting with the big number (37), and working our way down to 2, <i>decrementing <\/i>the value of i by 1 each time. Make sense? And that\u2019s what Step -1 does; it basically subtracts 1 each time the loop runs. <\/p>\n<p>Inside the loop itself we do two things. First, we have to create a string consisting of <i>i<\/i> number of blank spaces. Fortunately, we can do that with one line of code thanks to the Space function:<\/p>\n<pre class=\"codeSample\">strChars = Space(i)\n<\/pre>\n<p>Second, we need to see if the substring consisting of <i>i<\/i> number of blank spaces can be found anywhere in strStarter. If it <i>can<\/i>, we need to replace it with a single blank space. And that\u2019s what this code does:<\/p>\n<pre class=\"codeSample\">strStarter = Replace(strStarter, strChars, \" \")\n<\/pre>\n<p>From there we continue looping until we\u2019ve checked for 2 consecutive blank spaces. We then exit the loop, use the Split command to divide the string into an array of individual words, and then &#8211; just to show you that everything worked &#8211; we echo back those individual words.<\/p>\n<p>Now, this isn\u2019t totally foolproof; for example, what if you had a few extraneous spaces in <i>front<\/i> of the word <i>VBScript<\/i>? We didn\u2019t bother with situations like that, but you can always use VBScript\u2019s LTrim and RTrim functions to get rid of leading and trailing spaces. If you\u2019d like more information about string manipulation, check out this portion of the <a href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_vbs_vcgg.mspx\" target=\"_blank\"><b>Microsoft Windows 2000 Scripting Guide<\/b><\/a><b>.<\/b><\/p>\n<p>We also don\u2019t know the context in which a string like <i>VBScript is fun!<\/i> might arise. We suspect you might encounter strings like this when trying to read a fixed-width log file. If that\u2019s the case, the code above will work, but you might find it easier to use <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/dnclinic\/html\/scripting03092004.asp\" target=\"_blank\"><b>ADO (ActiveX Database Objects)<\/b><\/a> to parse the file instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know that I can use the Split command to separate a sentence like this &#8211; VBScript is fun! &#8211; into an array of individual words. However, what happens if I have a sentence like this: VBScript is fun! I can\u2019t use a blank space as the delimiter because I might have lots of blank [&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-71423","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>I know that I can use the Split command to separate a sentence like this &#8211; VBScript is fun! &#8211; into an array of individual words. However, what happens if I have a sentence like this: VBScript is fun! I can\u2019t use a blank space as the delimiter because I might have lots of blank [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71423","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=71423"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71423\/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=71423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}