|
|
Title | Build a function to compress consecutive spaces into one space in Visual Basic 2005 |
Description | This example shows how to build a function to compress consecutive spaces into one space in Visual Basic 2005. |
Keywords | space, spaces, compress, replace, Visual Basic .NET, Visual Basic 2005 |
Categories | Strings |
|
|
Thanks to Mariusz S.
The CompessSpaces function loops and, as long as the string contains two consecutive separator characters, it replaces two instances of the character with one instance.
|
|
Private Function CompressSpaces(ByVal s As String, Optional _
ByVal sep As String = " ") As String
s = Trim(s)
'function logic: as long as there are occurences of
' next-to-each-other separators in any number (at least
' two)
'then replace it with one occurence of such a sep
'so the function sort of contracts input string
' gradually
Do While InStr(s, sep & sep) > 0
s = Replace(s, sep & sep, sep)
Loop
CompressSpaces = s
End Function
|
|
|
|
|
|