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:
- How to Strip special characters from a given String
- How to strip out leading and trailing characters (Extended Trim)
- How to trim characters from the Beginning or End of a string
- How to trim characters from the End of a string
- How to trim characters from the Beginning of a string
- How to Replace Multiple Substrings in a String at Once
- How to check if a string contains only Alphabetical Characters
- How to check if a string contains only Numeric Characters
- How to Remove all numeric characters from a string
- How to Remove all Non Alpha Numeric characters from a given string