CommonPrefix - Maple Help

Online Help

All Products    Maple    MapleSim

StringTools

 CommonPrefix
 return the length of the longest common prefix of two strings
 CommonSuffix
 return the length of the longest common suffix of two strings

 Calling Sequence CommonPrefix( s1, s2 ) CommonSuffix( s1, s2 )

Parameters

 s1 - Maple string s2 - Maple string

Description

 • The CommonPrefix(s1, s2) command returns the length of the longest common prefix of its input strings, s1 and s2.
 The actual common prefix can be obtained by indexing into either of the strings, s1 or s2, with the range 1 .. CommonPrefix( s1, s2 ).
 Note: String t is a prefix of string s if $t={s}_{1..n}$, for some integer $1\le n\le \mathrm{length}\left(s\right)$, or t is the empty string. For example, the prefixes of the string "abc" are "", "a", "ab", and "abc".
 • The CommonSuffix(s1, s2) function returns the length of the longest common suffix of its input strings, s1 and s2.
 This function can be defined, in terms of CommonPrefix and Reverse as

$\mathrm{CommonSuffix}\left(\mathrm{s1},\mathrm{s2}\right)=\mathrm{CommonPrefix}\left(\mathrm{Reverse}\left(\mathrm{s1}\right),\mathrm{Reverse}\left(\mathrm{s2}\right)\right)$

 • 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{CommonPrefix}\left("abc","ab"\right)$
 ${2}$ (1)
 > $\mathrm{CommonPrefix}\left("abc","xab"\right)$
 ${0}$ (2)
 > $\mathrm{CommonPrefix}\left("","ab"\right)$
 ${0}$ (3)
 > $s≔"abc":$
 > $t≔"ab":$
 > s[ 1 .. CommonPrefix( s, t ) ];
 ${"ab"}$ (4)
 > t[ 1 .. CommonPrefix( s, t ) ];
 ${"ab"}$ (5)
 > $\mathrm{CommonSuffix}\left("abc","xbc"\right)$
 ${2}$ (6)
 > $\mathrm{CommonSuffix}\left("abc","xbcd"\right)$
 ${0}$ (7)

 See Also