Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleBuild a function to compress consecutive spaces into one space in Visual Basic 2005
DescriptionThis example shows how to build a function to compress consecutive spaces into one space in Visual Basic 2005.
Keywordsspace, spaces, compress, replace, Visual Basic .NET, Visual Basic 2005
CategoriesStrings
 
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
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated