
Equivalent functionality to trim - but with a differenct character set trimmed
A slight adjustement for exception case of empty string...
Function RTrimChars(theString, theChars)
Dim ptr
RTrimChars = vbNullString
ptr = len(theString)
If ptr = 0 Then Exit Function
Do While Instr(theChars,Mid(theString,ptr,1)) > 0
ptr = ptr-1 : If ptr = 0 Then Exit Do
Loop
if ptr > 0 Then
RTrimChars = Mid(theString,1,ptr)
End If
End Function
--
Michael Harris
Microsoft.MVP.Scripting
' Subset of chars to be trimmed: "abcde"
'
' String to process: "14a" Result: "14"
' String to process: "14a9ce" Result: "14a9"
' String to process: "14a3e22" Result: "14a3e22"
' String to process: "1abcXeab" Result: "1abcX"
strChars = "abcde"
strTest = "14a"
WScript.Echo _
"String: " & strTest & vbCrLf,_
"Result: " & RTrimChars(strTest,strChars)
strTest = "14a9ce"
WScript.Echo _
"String: " & strTest & vbCrLf,_
"Result: " & RTrimChars(strTest,strChars)
strTest = "14a3e22"
WScript.Echo _
"String: " & strTest & vbCrLf,_
"Result: " & RTrimChars(strTest,strChars)
strTest = "1abcXeab"
WScript.Echo _
"String: " & strTest & vbCrLf,_
"Result: " & RTrimChars(strTest,strChars)
strTest = "abcde"
WScript.Echo _
"String: " & strTest & vbCrLf,_
"Result: " & RTrimChars(strTest,strChars)
Function RTrimChars(theString, theChars)
Dim ptr
ptr = len(theString)
Do While Instr(theChars,Mid(theString,ptr,1)) > 0
ptr = ptr-1 : If ptr = 0 Then Exit Do
Loop
if ptr > 0 Then
RTrimChars = Mid(theString,1,ptr)
Else
RTrimChars = vbNullString
End If
End Function
--
Michael Harris
Microsoft.MVP.Scripting
Quote:
> I need to perform some string processing, which involves removing
> a certain subset of characters from a string, but only those that
> occur after the last character not contained in the subset.
> Effectively it must behave like an RTrim, but the trimmed
> characters are not whitespace, but an alternate selection of
> characters. For example:
> Subset of chars to be trimmed: "abcde"
> String to process: "14a" Result: "14"
> String to process: "14a9ce" Result: "14a9"
> String to process: "14a3e22" Result: "14a3e22"
> String to process: "1abcXeab" Result: "1abcX"
> I know how to test whether each character in the processed string
> appears in the trim subset. The main problem I am having is I
> can't think of a way to remove only those appearing to the right
> of the last non-subset character.
> I figure this may be really short and simple with regular
> expressions, but I haven't got my head around all that yet.
> Thanks for your help,
> Alex Cross.