 StringTools - Maple Programming Help

Home : Support : Online Help : Programming : Names and Strings : StringTools Package : Pattern Matching : StringTools/Subs

StringTools

 Subs
 effect multiple substitutions in a string

 Calling Sequence Subs( [ L1 = R1, L2 = R2, ... ], s ) Subs( { L1 = R1, L2 = R2, ... ], s ) Subs( L = R, s )

Parameters

 s - string; the string in which to effect substitutions L, L1, L2, ... - string; left hand sides of substitutions; substrings to replace R, R1, R2, ... - string; right hand sides of substitutions; replacements for corresponding left hand sides

Description

 • The Subs command effects (single or) multiple substitutions in a string s as specified by its first argument. The precise way in which those substitutions take place depends upon the form of the first argument, which may be a single equation of the form L = R, or a set or list of such equations.
 • If the first argument is a single equation L = R, then Subs( L = R, s ) simply replaces occurrences of the substring L in s by the string R, by using the command StringTools:-SubstituteAll( s, L, R ).
 • When a list of substitutions is presented as the first argument, then the substitutions in the list are performed serially, in the order specified. This means that the first substitution L1 = R1 is applied to the string s, using the command StringTools:-SubstituteAll( s, L1, R1 ). Then, the substitution L2 = R2 is applied to the result; then the substitution L3 = R3 is applied to the result of that, and so on.
 • When the substitutions are given as a set, then parallel substitutions are effected, with the "longest-match rule" being used to resolve multiple matches in the input string s. This means that, if two of the left hand sides among L1, L2, ... match a substring of s at the same position, then the longest such match is used in the substitutions.
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings.

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{Subs}\left("ab"="XY","uvabbaabcst"\right)$
 ${"uvXYbaXYcst"}$ (1)

Note the difference between serial and parallel substitutions

 > $\mathrm{Subs}\left(\left\{"ab"="XY","abc"="QRST"\right\},"uvabbaabcst"\right)$
 ${"uvXYbaQRSTst"}$ (2)
 > $\mathrm{Subs}\left(\left["ab"="XY","abc"="QRST"\right],"uvabbaabcst"\right)$
 ${"uvXYbaXYcst"}$ (3)

The following example illustrates the significance of the order of substitutions in a list.

 > $\mathrm{Subs}\left(\left["Yc"="FOO","ab"="XY","abc"="QRST"\right],"uvabbaabcst"\right)$
 ${"uvXYbaXYcst"}$ (4)
 > $\mathrm{Subs}\left(\left["ab"="XY","abc"="QRST","Yc"="FOO"\right],"uvabbaabcst"\right)$
 ${"uvXYbaXFOOst"}$ (5)

Compatibility

 • The StringTools[Subs] command was updated in Maple 2017.