MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: a conflicting StringReplace

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56320] Re: a conflicting StringReplace
  • From: Marcus Stollsteimer <marcus314 at yahoo.com>
  • Date: Fri, 22 Apr 2005 06:22:44 -0400 (EDT)
  • Organization: Comp.Center (RUS), U of Stuttgart, FRG
  • References: <d47tic$578$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hui Fang wrote:

> No problem on those. Now a student tried the following:
> In[3]   StringReplace["abc", {"ab"->"AB", "bc"->"BC"}]
> Out[3]   ABc
> 
> Now he asked me why only "ab" is replaced. I said this is because
> there is a conflict because both "ab" and "bc" contains "b". So
> Mathematica will choose the first replacement. I also told him if he
> changes the order, he will get aBC. Now:
> In[4]    StringReplace["abc", {"bc"->"BC","ab"->"AB"}]
> Out[4]    ABc
> 
> This is the part I don't understand. Does Mathematica treat those
> rules in their canonical order (since "ab" is before "bc" in
> canonical order.), or in their written order?

StringReplace first tries to apply the rules at position 1 of
the string, then goes on to the next character. Therefore
always "ab" will be replaced.

From the help for StringReplace[]:

"StringReplace goes through a string, testing substrings that start at
each successive character position. On each substring, it tries in
turn each of the transformation rules you have specified. If any of
the rules apply, it replaces the substring, then continues to go
through the string, starting at the character position after the end
of the substring."

Regards,
Marcus

-- 
Time enough to think of the future
when you havent any future to think of. -- George Bernard Shaw


  • Prev by Date: Re: multiple 3d plots
  • Next by Date: Re: a conflicting StringReplace
  • Previous by thread: Re: a conflicting StringReplace
  • Next by thread: Re: a conflicting StringReplace