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
 
 
 
 
 
TitleTrim leading and trailing non-printing ASCII characters from a string in Visual Basic 6
DescriptionThis example shows how to trim leading and trailing non-printing ASCII characters from a string in Visual Basic 6.
Keywordstrim, ltrim, rtrim, TrimWhitespace, LTrimWhitespace, RTrimWhitespace, non-printing, ASCII, carriage return, linefeed, line feed
CategoriesStrings, Tips and Tricks
 
Function LTrimWhitespace searches a string until it finds the first printable character. It then returns the string from that point on.

Function RTrimWhitespace searches a string from the end toward the beginning until it finds the first printable character. It then returns the string from that point on.

Function TrimWhitespace calls LRTrimWhitespace and RTrimWhitespace to remove non-printing characters from both ends of a string.

 
' Remove leading ASCII characters before " " and after "~".
Private Function LTrimWhitespace(ByVal txt As String) As _
    String
Dim ch As String
Dim i As Integer

    ' Assume we won't find any printable character.
    LTrimWhitespace = ""

    ' Find the first printable character.
    For i = 1 To Len(txt)
        ch = Mid$(txt, i, 1)
        If (ch >= " ") And (ch <= "~") Then
            LTrimWhitespace = Mid$(txt, i)
            Exit For
        End If
    Next i
End Function

' Remove trailing ASCII characters before " " and after "~".
Private Function RTrimWhitespace(ByVal txt As String) As _
    String
Dim ch As String
Dim i As Integer

    ' Assume we won't find any printable character.
    RTrimWhitespace = ""

    ' Find the first printable character.
    For i = Len(txt) To 1 Step -1
        ch = Mid$(txt, i, 1)
        If (ch >= " ") And (ch <= "~") Then
            RTrimWhitespace = Mid$(txt, 1, i)
            Exit For
        End If
    Next i
End Function

' Remove leading and trailing ASCII characters before " "
' and after "~".
Private Function TrimWhitespace(ByVal txt As String) As _
    String
    TrimWhitespace = LTrimWhitespace(RTrimWhitespace(txt))
End Function
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated