devxlogo

Enhance the Trim Function

Enhance the Trim Function

The Trim function has a serious shortcoming: It handles only space characters?not all the usual white spaces such as tabs, carriage returns, and line feeds. Instead of the standard Trim function, use my TrimAll function, which handles all white spaces. In fact, you can extend it to trim off any character by editing the assignment to the ToEliminate string variable:

 Public Function TrimAll(ToTrim As String) As String	Static ToEliminate As String	Dim Start As Long, Finish As Long	' Base condition test	If Len(ToTrim) = 0 Then		TrimAll = ""		Exit Function	End If	' Define the characters (once) that we want to trim off	If Len(ToEliminate) = 0 Then		ToEliminate = Chr(0) & Chr(8) & Chr(9) _			& Chr(10) & Chr(13) & Chr(32)	End If	' Find the beginning of nonblank string by checking  	' each char against a list of blank chars.	Start = 1	Do While Start  1		If InStr(ToEliminate, Mid$(ToTrim, Finish, 1)) Then				Finish = Finish - 1		Else			Exit Do		End If	Loop	If Start > Finish Then		' If the string is completely blank,		' Start will be greater than Finish.		TrimAll = ""		Exit Function	Else		' Trim out the real contents		TrimAll = Mid$(ToTrim, Start, Finish - Start + 1)	End IfEnd Function

devx-admin

Share the Post: