Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Nov 18, 1999



Application Security Testing: An Integral Part of DevOps

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 Function

Public 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
		RTrim = Left$(inString, nPos)
	End If
End Function

Public 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
		LTrim = Mid$(inString, nPos)
	End If
End Function
John Cullen
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date