StringTricks 1.29

documentation notebook

Eric Rowland
https://ericrowland.github.io/packages.html

Introduction

StringTricks is a collection of string manipulation and analysis tools.
This introduction gets you started with some features of the package; the next section provides a complete list of package symbols along with their usage messages and further examples.

To use StringTricks, first you will need to load the package by evaluating the following cell. (If you need help, see loading a package.)

<<StringTricks`

General string manipulation

StringTricks provides string analogues of the ReplaceRepeated and Tuples.

Generate the beginning of the Thue–Morse sequence:

StringReplaceRepeated["0",{"0"->"01","1"->"10"},6]

StringTuples gives all words of a given length on a given alphabet:

StringTuples[{"b","a","t"},3]

Repetitions

Determine the period length of a repeating string:

StringPeriodLength["repeatrepeatrepeatrep"]

Compute the exponent:

StringExponent["repeatrepeatrepeatrep"]

The string is a 7/2-power:

StringPowerQ["repeatrepeatrepeatrep",7/2]

Parsing

Parsing nested expressions is a common string manipulation task.  MatchPairs finds matching pairs of nested infix operators.

string="pre start stuff start a end lots more stuff start b end junk! end";

MatchPairs[string,{"start","end"}]

Column[(StringTake[string,{#1[[1,1]],#1[[2,2]]}]&)/@%]

Clear[string]

Package symbols

MatchPairs

?MatchPairs

Extract each bracketed substring:

MatchPairs["(()())",{"(",")"}]

(StringTake["(()())",{#1[[1,1]],#1[[2,2]]}]&)/@%

A non-syntactic string:

MatchPairs[")))((()()(",{"(",")"}]

Switching the interpretation of the bracketing produces a syntactic string in this case:

MatchPairs[")))((()()(",{")","("}]

MatchPairs["Let $f(x) = x^2$ for $x \\geq 0$.","$"]

RunLengths

?RunLengths

RunLengths["aaabbAB"]

StringAgreementLength

?StringAgreementLength

StringAgreementLength["abcd","abce"]

StringAgreementLength[IntegerString[1072934],IntegerString[1072]]

StringExponent

?StringExponent

StringExponent["abcabc"]

StringExponent["abcab"]

The string "000" is both a cube and a 3/2-power, but StringExponent gives the larger of the two:

StringPowerQ["000",#]&/@{(3/2),3}

StringExponent["000"]

StringPartitionAt

?StringPartitionAt

StringPartitionAt["abcdefgh",2]

StringPartitionAt["abcdefgh",{3,4,5,7,8}]

StringPeriodLength

?StringPeriodLength

StringPeriodLength["abcabc"]

StringPeriodLength["abcab"]

StringPower (a generalization of StringRepeat)

?StringPower

StringPower["a",10]

StringPower["010",10]

StringPower["010",10/3]

Column[Table[StringPower["abcdef",n],{n,0,4,(1/3)}]]

StringPowerQ

?StringPowerQ

StringPowerQ["abcabc"]

StringPowerQ["abcab"]

Find string powers:

Select[Join@@Table[StringTuples[{"0","1"},n],{n,0,4}],StringPowerQ]

Words that are not integral powers are sometimes called primitive.

Select[Join@@Table[StringTuples[{"0","1"},n],{n,0,4}],!StringPowerQ[#1]&]

Test whether a word is a fractional power:

StringPowerQ["abcab",(5/3)]

StringPower["abc",(5/3)]

StringReplaceRepeated

?StringReplaceRepeated

Cancel inverses in a group element:

StringReplaceRepeated["aaBaBAA"<>"aababAB",{"aA"->"","bB"->"","Aa"->"","Bb"->""}]

Perform replacements on overlapping substrings that do not change the characters on which they overlap:

StringReplaceRepeated["{{a,b,c,d}{a,b}{a,b,c}}",{"}{"->"},{","{a,"->"{"}]

The Fibonacci morphism:

StringReplaceRepeated["0",{"0"->"01","1"->"0"},8]

Truncating at a certain length lets one easily compute the prefix of a fixed point:

StringReplaceRepeated["0",{"0"->"011","1"->"010"},∞,100]

StringTuples

?StringTuples

Find all words on three letters:

StringTuples[{"a","b","c"},3]

StringTuples[{"01","011"},3]

StringTuples[{"01","011"},{0,2}]