|
功能类似 LTRIM RTRIM TRIM,但可去掉任何指定的头尾字符.
作者: r_cubed
摘抄: tmtony
Public Function GTrim(strInput As String, _
strCharToStrip As String, _
WhatToTrim As String) _
As String
' This function is in addition to the Access functions
' LTrim and RTrim which trim off leading/trailing spaces from a field/string.
' The GTRIM function allows you to trim off ANY leading and/or trailing character
' OR "set of characters" (i.e. it is NOT restricted to only trimming off a
' SINGLE character at a time.
' Inputs: Parameter 1: String to be trimmed
' Parameter 2: Leading/Trailing character/s to be trimmed
' Parameter 3: L - Trim leading
' R - Trim trailing
' B - Trim leading AND trailing
' Examples: GTRIM("abcabcdefxyzabc","abc","B") returns "defxyz"
' GTRIM("abcabcdefxyzab","abc","B") returns "defxyzab"
' GTRIM("000321A","000","L") returns "321A"
' GTRIM("00321A","000","L") returns "00321A" ** No change/no match
' GTRIM(" Hello World "," ","B") returns "Hello World" ** Same as LTrim, RTrim
' Usage: The function can be used anywhere that you can use ANY other function
'
' ------------------------------------------------------------------------------------------
Dim i As Integer
If WhatToTrim = "L" _
Or WhatToTrim = "B" Then
' Attempt to trim the leftside of the input string
For i = 1 To Len(strInput) Step Len(strCharToStrip)
If Mid$(strInput, i, Len(strCharToStrip)) <> strCharToStrip Then
Exit For
End If
Next i
End If
If i < Len(strInput) Then
strInput = Mid$(strInput, i)
End If
If WhatToTrim = "R" _
Or WhatToTrim = "B" Then
' Attempt to trim the rightside of the input string
i = Len(strInput) - Len(strCharToStrip) + 1
For i = i To 1 Step -Len(strCharToStrip)
If Mid$(strInput, i, Len(strCharToStrip)) <> strCharToStrip Then
Exit For
End If
Next i
End If
If i >= 1 Then
i = i + Len(strCharToStrip) - 1
strInput = Mid$(strInput, 1, i)
End If
GTrim = strInput
End Function
|
|