help - tricky use of "replace" 
Author Message
 help - tricky use of "replace"

You are probably better off with a regular expression:

Set Reg = New Regexp
Reg.Global = True
Reg.IgnoreCase = True
Reg.Pattern = "[a-c]"
NewString = Reg.Replace(OldString,"d")

The above regular expression will search the string OldString for all
characters in the range a-c (a,b,c) ,ignoring case, and replace any of
those characters with d. Setting Global to True ensures that it
replaces all matches in the string rather than stopping at the first
match.


: I need to replace multiple characters in the same string, and it
cannot be
: done (in a straightforward manner) as a set of sequential individual
: replacements (ie. replace "A" with "B", then "C" with "D", then "E"
with "F"
: etc.), because some of the replacements will mess up subsequent
replacements
: (the characters to search for overlap with their replacment values).
:
: So, is there some syntax to say, in a single line, "Replace A or B
or C with
: D?"  That would do it for me.
:

:
:



Mon, 15 Jul 2002 03:00:00 GMT  
 help - tricky use of "replace"
Your choices are:

1. (Not always possible) Rearrange the replace statements so there is no
overlap.

2. (Not always possible) Do the replacement in two stages. First with

replace those characters with the ones you want.

3. Go through the string character by character, replacing each one. Since
each character is processed only once, there is no danger of overlap.

  Function Translate(strIn)
    strOut=""
    For I = 1 to Len(strIn)
      chrIn = Mid(strIn, I, 1)
      Select Case chrIn
        Case "A"
          strOut = strOut & "B"
        Case "B"
          strOut = strOut & "A"
        Case "C"
          strOut = strOut & "R"
        Case "D"
          strOut = strOut & "C"
        Case "R"
          strOut = strOut & "X"
        ' ...
        Case Else
          strOut = strOut & chrIn
      End Select
    Next 'I
    Translate = strOut
  End Function

4. (Added for completeness) Download ActivePerl from www.activestate.com and
rewrite the script in Perl, using the tr statement.

=-=-=
Steve
-=-=-


Quote:
> I need to replace multiple characters in the same string, and it cannot be
> done (in a straightforward manner) as a set of sequential individual
> replacements (ie. replace "A" with "B", then "C" with "D", then "E" with "F"
> etc.), because some of the replacements will mess up subsequent replacements
> (the characters to search for overlap with their replacment values).

> So, is there some syntax to say, in a single line, "Replace A or B or C with
> D?"  That would do it for me.





Tue, 16 Jul 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Using "Replace" on ADODB.Field

2. "Tricky" hint replacements

3. Tricky "FindWindow" question

4. Tricky "FindWindow" question

5. "HELP!" finding search/replace routine

6. CSng(Replace("","","")) = 0.0?

7. How to Replace("String Irregular # of space", "?", "")

8. Replacing "Me" in a standard module

9. "Replace all "

10. replace null in field with "-"

11. Find Replace "my way"

12. macro for "replace" in word

 

 
Powered by phpBB® Forum Software