Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
500MB 27GB Web Hosting - $9.95/Month
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
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-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated