The Visual Basic Trim$() function has a serious shortcoming in that it only handles space characters and not all of the usual white spaces such as tabs, carriage returns, and line feeds. The following functions is interchangeable with the standard Trim , but will handle all of the white spaces. In fact, if you look […]
CodeGuru content and product recommendations are
editorially independent. We may make money when you click on links
to our partners.
Learn More
The Visual Basic Trim$() function has a serious shortcoming in that it only handles space characters and not all of the usual white spaces such as tabs, carriage returns, and line feeds.
The following functions is interchangeable with the standard Trim , but will handle all of the white spaces. In fact, if you look closely, you will discover that it can be configured to trim off any character that you would like.
public Function TrimAll(ToTrim as string) as string
Dim Start, Finish as Integer
Dim ToEliminate as string
If len(ToTrim) = 0 then
TrimAll = ""
Exit Function
End If
ToEliminate = Chr(0) & Chr(8) & Chr(9) & Chr(10) & _
Chr(13) & Chr(32)
Start = 1
While InStr(1, ToEliminate, mid$(ToTrim, Start, 1), _
vbTextCompare) <> 0 And Start <= len(ToTrim)
Start = Start + 1
Wend
Finish = len(ToTrim)
While InStr(1, ToEliminate, mid$(ToTrim, Finish, 1), _
vbTextCompare) <> 0 And Finish > 1
Finish = Finish - 1
Wend
If Start > Finish then
TrimAll = ""
Exit Function
End If
TrimAll = mid$(ToTrim, Start, Finish - Start + 1)
End Function