String Manipulation

We develop some functions for manipulating strings. A string in Mathematica is
a collection of characters in double quotes ". For example,

s= "abced01234567"

is a string. There are many functions for manipulating strings. Here is a brief list
of the most useful functions.

Characters[string] gives a list of characters in a string and
StringJoin[ list] returns a string from the specified set of characters.

In[8]:=

  Characters[ "abcdefg"]

Out[8]

In[9]:=

  StringJoin[ %]

Out[9]

The ASCII character codes for each character can be obtained from
ToCharacterCode[ string]
and the string form of a list of character codes is obtained using
FromCharacterCode. These functions are useful is manipulating strings
to perform arithmetic operations on them.

Additional functions that are useful for String manipulation are the following.

StringLength[ s] Gives the number of characters in s.

StringTake[ s,k] Gives the first k characters of s.

StringTake[ s, {k,l}] Gives the characters from positions k through l.

StringPosition[ s, t] Gives a list of position where string t occurs in string s.

StringDrop[ s, {k,l}] Returns a string with the characters from positions k through l of
s removed.

Strings can be joined using StringJoin[ s1,s2] or s1<>s2.

StringReplace[ s, { s1->t1,....}] will replace all occurrences of s1 by t1 in s.

We illustrate these with the following examples.

Shift Cipher:

Frequency Analysis:

Up to Procedural Programming