Make More Versatile Trim Functions

Make More Versatile Trim Functions

One of the nice things about VB is the ability to redefine most of the built-in commands. For example, you can extend the functionality of the Trim family of commands. LTrim and RTrim remove leading and trailing spaces from a string, but it also would be useful to remove other nonprinting characters, such as tabs, or carriage return/line feed pairs that might be present after reading in a text file. This code does exactly that by removing all leading and trailing characters with an ASCII value less than or equal to that of a space character:

 Public Function Trim(ByVal inString As String) _	As String	Trim = RTrim(LTrim(inString))End FunctionPublic Function RTrim(ByVal inString As String) _	As String	Dim nPos As Long	nPos = Len(inString)	If nPos > 0 Then		Do While (Asc(Mid$(inString, _			nPos, 1)) <= 32)			nPos = nPos - 1			If nPos = 0 Then Exit Function		Loop		RTrim = Left$(inString, nPos)	End IfEnd FunctionPublic Function LTrim(ByVal inString As String) _	As String	Dim nPos As Long, nLen As Long	nLen = Len(inString)	If nLen > 0 Then		nPos = 1		Do While Asc(Mid$(inString, nPos, 1)) <= 32			nPos = nPos + 1			If nPos = nLen Then Exit Function		Loop		LTrim = Mid$(inString, nPos)	End IfEnd Function
See also  Is Machine Learning Automating Creativity in Graphic Design?

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