|
|
Title | Trim leading and trailing non-printing ASCII characters from a string in Visual Basic 6 |
Description | This example shows how to trim leading and trailing non-printing ASCII characters from a string in Visual Basic 6. |
Keywords | trim, ltrim, rtrim, TrimWhitespace, LTrimWhitespace, RTrimWhitespace, non-printing, ASCII, carriage return, linefeed, line feed |
Categories | Strings, 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
|
|
|
|
|
|