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 <= Len(ToTrim)		If InStr(ToEliminate, Mid$(ToTrim, Start, 1)) Then			Start = Start + 1		Else			Exit Do		End If	Loop	' Find the end of nonblank string.	Finish = Len(ToTrim)	Do While Finish > 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
See also  11 Effective Keyword Research Tools and Techniques

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist