| Answer: |
There are a number of techniques that can be used to remove extraneous spaces from a string. VBScript includes a couple of functions to help remove leading and/or trailing spaces from a string. LTrim removes leading spaces, RTrim removes trailing spaces, and Trim removes both leading and trailing spaces. For example:
Dim str str = " Leading and trailing spaces... "
str = Trim(str)
'At this point str = "Leading and trailing spaces..."
|
While these trimming functions are nice, they don't remove extraneous spaces from between words in the string. For example, if we had the string:
This is a test.
We might want to remove those extra between-word spaces, converting the string to:
This is a test
Unfortunately there is no built-in VBScript function that will do this for us. However, it is not too difficult to write our own code to handle this. There are two simple methods for doing this, the first uses the VBScript Replace function to continually replace all instances of two consecutive spaces with one space until there are no instances of two consecutive spaces in the string.
'Start by trimming leading/trailing spaces str = Trim(str)
'Now, while we have 2 consecutive spaces, replace them 'with a single space... Do While InStr(1, str, " ") str = Replace(str, " ", " ") Loop
|
Be sure to view the live demo of this function!
Another way to accomplish the same feat is to use regular expressions. (For information on regular expressions be sure to visit the Regular Expressions Article Index.) With regular expressions we can say, "Find occurrences of two of more whitespace characters (tabs, spaces, etc.) and replace it with a single space." The regular expression for finding a space character is \s. To specify that we want to match two or more occurrences of the space character we use: \s{2,}. Here is the source code:
Dim regEx Set regEx = New RegExp regEx.Global = true regEx.IgnoreCase = True
regEx.Pattern = "\s{2,}"
str = Trim(regEx.Replace(str, " "))
|
Be sure to view the live demo of this function!
Either of these two methods can be used to remove extraneous spaces from between the words of a string.
Alert ASPFAQs.com reader Eveline S. wrote in...
|
Thanks for sharing the code alternatives. They are very useful for the "ASP newbie", like me, and very much appreciated.
I do have one question, though. I tested the code on the pages you provided: " I think this is great !" returns "I think this is great !"
My question: The words are correctly separated by a space, but how do you make sure there is no space between a word and the following punctuation (i.e., "great !" versus "great!")?
The solution is to use a second regular expression search and replace after you've used the first one. This second one replaces any space followed by a punctuation mark with just the punctuation mark. |
To accomplish this, the regular expression to use is:
\s(\!|\.|\?|\;|\,|\:) which looks for a space (\s) followed by any punctuation mark. To replace the match with the proper punctuation mark, you must use a back-reference - $1:
regEx.Pattern = "\s(\!|\.|\?|\;|\,|\:)"
str = regEx.Replace(str, "$1")
|
This would be integrated into our earlier example, immediately following the first regular expression replace. Therefore, our complete code would look like:
<% str = " This is a , test fool ! "
'Remove extraneous spaces Dim regEx, Match, Matches Set regEx = New RegExp regEx.Global = true regEx.IgnoreCase = True regEx.Pattern = "\s{2,}"
str = Trim(regEx.Replace(str, " "))
'Remove the space between the end of a word and 'a punctuation mark regEx.Pattern = "\s(\!|\.|\?|\;|\,|\:)" str = regEx.Replace(str, "$1") %>
|
Neat, eh? For an example of using the back-reference ($1) check out Common Applications of Regular Expressions. Happy Programming! |