CodeItBetter Programming Another VB Programming Blog

How to Trim all Non-printing Characters from a given String

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'String Manipulation - How to Trim all Non-printing Characters from a given String
Option Explicit
 
'This function trims all nonprinting characters (tabs, character returns, spaces, etc.)

Public Function TrimAll(ByVal sString As String) As String
    Dim sResult As String
    Dim lLength As Long, lCount As Long
 
    sResult = sString
    lLength = Len(sString)
 
    If lLength > 0 Then
        'Ltrim
        For lCount = 1 To lLength
            If Asc(Mid$(sResult, lCount, 1)) > 32 Then Exit For
        Next lCount
 
        sResult = Mid$(sResult, lCount)
        lLength = Len(sResult)
 
        'Rtrim
        If lLength > 0 Then
            For lCount = lLength To 1 Step -1
                If Asc(Mid$(sResult, lCount, 1)) > 32 Then Exit For
            Next lCount
        End If
        sResult = Left$(sResult, lCount)
    End If
    TrimAll = sResult
End Function

Related posts:

  1. How to Strip special characters from a given String
  2. How to strip out leading and trailing characters (Extended Trim)
  3. How to trim characters from the Beginning or End of a string
  4. How to trim characters from the End of a string
  5. How to trim characters from the Beginning of a string
  6. How to Replace Multiple Substrings in a String at Once
  7. How to check if a string contains only Alphabetical Characters
  8. How to check if a string contains only Numeric Characters
  9. How to Remove all numeric characters from a string
  10. How to Remove all Non Alpha Numeric characters from a given string

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.