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
 
 
 
 
 
TitleStrip out extra tabs, CrLf and multiple spaces
Keywordsclean, string
CategoriesStrings
 
Thanks to Alex Stewart

Use Replace to convert the target characters (tab, vbCrLf, etc.) into spaces. Then while there are multiple space combinations, use Replace to convert double spaces into single spaces.

 
Public Function CleanString(strSource As String) As String
    On Error GoTo CleanStringErr

    ' convert tabs to spaces first
    strSource = Replace(strSource, vbTab, " ")

    ' convert all CRLFs to spaces
    strSource = Replace(strSource, vbCrLf, " ")

    ' Find and replace any occurences of multiple spaces
    Do While (InStr(strSource, "  "))
        ' if true, the string still contains double spaces,
        ' replace with single space
        strSource = Replace(strSource, "  ", " ")
    Loop

    ' Remove any leading or training spaces and return
    ' result
    CleanString = Trim(strSource)
    Exit Function

CleanStringErr:
    ' Insert error-handling code here
End Function
 
See also Compress spaces in a string.

Formatted by Neil Crosby

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated