Equivalent functionality to trim - but with a differenct character set trimmed 
Author Message
 Equivalent functionality to trim - but with a differenct character set trimmed

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.



Sat, 01 May 2004 07:58:40 GMT  
 Equivalent functionality to trim - but with a differenct character set trimmed
'  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.



Sat, 01 May 2004 11:38:02 GMT  
 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.



Sat, 01 May 2004 11:46:33 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Difference between trim and trim$

2. Trim not trimming?

3. Trim() vs Trim$(), etc

4. Trim vs Trim$

5. equivalent of vbsscript "trim"

6. java script equivalent for trim()

7. Equivalent javascript function as Trim() of VB Script.

8. JScript equivalent of VBScript 'Trim()'?

9. Equivalent of the VBScript Trim() function

10. Underlining in body of cross tab report and trimming characters

11. String field got trimmed to 255 characters

12. trim( ) not eliminating some invisible character

 

 
Powered by phpBB® Forum Software