TrimEx:
Trims a string of all leading and trailing whitespace.
Description:
Trims a string of all leading and trailing whitespace. Works better than VBScript's Trim() function which doesn't always trim every possible whitespace character. Uses Regular Expression Object that requires vbScript 5.1 or higher.
Syntax:
stringTrimmed = TrimEx(stringToTrim)
Details:
The example below returns:
"hi there"
Example:
<%
dim string1, string2
string1 = " hi there " string2 = TrimEx(string1)
Response.Write string2
%>
ASP Source Code:
<%
Function TrimEx(byval str) Dim loRegExp '--- Regular Expression Object (Requires vbScript 5.1 or higher) Dim boolErr On Error Resume Next '--- Create Regular Expression object Set loRegExp = New RegExp '--- Error handling If Err Then boolErr = True End If On Error GoTo 0 If boolErr then Err.Raise 5108, "TrimEx Function", "This function uses " & _ "the RegExp object and requires the VBScript " & _ "Scripting Engine Version 5.1 or higher." StripHTMLRegExp = Null Exit Function End If With loRegExp '--- Keep finding spaces after the first one. .Global = True '--- Ignore upper/lower case .IgnoreCase = True '--- Look for spaces .Pattern = "^\s+|\s+$" TrimEx = .replace(str, "") End With '--- Release regular expression object Set loRegExp = Nothing End Function '--- TrimEx
%>
|