LISTING 1: Code That Illustrates the SuperTrim Function

BEGIN COMMENT
' To demonstrate SuperTrim, the following code uses the
' script component runtime library (scrobj.dll) to create a
' globally unique identifier (GUID). A bug in the library returns the
' GUID with two null characters at the end.
END COMMENT
guid = CreateObject("Scriptlet.TypeLib").Guid
BEGIN COMMENT
' Although only 38 characters are visible, VBScript's Len function
' shows that 40 characters exist.
END COMMENT
WScript.Echo Len(guid), "characters:", guid
BEGIN COMMENT
' Using the Trim function doesn't remove the extra characters.
END COMMENT
guid = Trim(guid)
WScript.Echo Len(guid), " characters after using Trim()."
BEGIN COMMENT
' Using the SuperTrim function removes the extra characters.
END COMMENT
guid = SuperTrim(guid)
WScript.Echo Len(guid), " characters after using SuperTrim()."

BEGIN COMMENT
' The SuperTrim function removes characters with ASCII values
' lower than 33 from the beginning and end of a string. If the value
' is null, the code returns a null just as Trim() does.
END COMMENT
Function SuperTrim(ByVal s)
	If IsNull(s) Then
		SuperTrim = s
	Else
		Do While Len(s) > 0
			If Asc(Left(s, 1)) < 33 Then
				s = Mid(s, 2)
			ElseIf Asc(Right(s, 1)) < 33 Then
				s = Left(s, Len(s) - 1)
			Else
BEGIN COMMENT
				' The script has no special characters
' to trim. Exit the loop.
END COMMENT
				Exit Do
			End If
		Loop
		SuperTrim = s
	End If
End Function