Operations on Strings

The Wolfram Language provides a variety of functions for manipulating strings. Most of these functions are based on viewing strings as a sequence of characters, and many of the functions are analogous to ones for manipulating lists.

Operations on complete strings.

You can join together any number of strings using <>:

"aaaaaaa"<>"bbb"<>"cccccccccc"

(RefLink[StringLength, paclet:ref/StringLength]) gives the number of characters in a string:

StringLength[%]

(RefLink[StringReverse, paclet:ref/StringReverse]) reverses the characters in a string:

StringReverse["A string."]

Taking and dropping substrings.

(RefLink[StringTake, paclet:ref/StringTake]) and (RefLink[StringDrop, paclet:ref/StringDrop]) are the analogs for strings of (RefLink[Take, paclet:ref/Take]) and (RefLink[Drop, paclet:ref/Drop]) for lists. Like (RefLink[Take, paclet:ref/Take]) and (RefLink[Drop, paclet:ref/Drop]), they use standard Wolfram Language sequence specifications, so that, for example, negative numbers count character positions from the end of a string. Note that the first character of a string is taken to have position 1.

Here is a sample string:

alpha="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

This takes the first five characters from alpha:

StringTake[alpha,5]

Here is the fifth character in alpha:

StringTake[alpha,{5}]

This drops the characters 10 through 2, counting from the end of the string:

StringDrop[alpha,{-10,-2}]

Inserting into a string.

(RefLink[StringInsert, paclet:ref/StringInsert])[s,snew,n] is set up to produce a string whose n(^th) character is the first character of snew.

This produces a new string whose fourth character is the first character of the string "XX":

StringInsert["abcdefgh","XX",4]

Negative positions are counted from the end of the string:

StringInsert["abcdefgh","XXX",-1]

Each copy of "XXX" is inserted at the specified position in the original string:

StringInsert["abcdefgh","XXX",{2,4,-1}]

This uses (RefLink[Riffle, paclet:ref/Riffle]) to add a space between the words in a list:

StringJoin[Riffle[{"cat","in","the","hat"}," "]]

Replacing parts of a string.

This replaces characters 2 through 6 by the string "XXX":

StringReplacePart["abcdefgh","XXX",{2,6}]

This replaces two runs of characters by the string "XXX":

StringReplacePart["abcdefgh","XXX",{{2,3},{5,-1}}]

Now the two runs of characters are replaced by different strings:

StringReplacePart["abcdefgh",{"XXX","YYYY"},{{2,3},{5,-1}}]

Finding positions of substrings.

You can use (RefLink[StringPosition, paclet:ref/StringPosition]) to find where a particular substring appears within a given string. (RefLink[StringPosition, paclet:ref/StringPosition]) returns a list, each of whose elements corresponds to an occurrence of the substring. The elements consist of lists giving the starting and ending character positions for the substring. These lists are in the form used as sequence specifications in (RefLink[StringTake, paclet:ref/StringTake]), (RefLink[StringDrop, paclet:ref/StringDrop]), and (RefLink[StringReplacePart, paclet:ref/StringReplacePart]).

This gives a list of the positions of the substring "abc":

StringPosition["abcdabcdaabcabcd","abc"]

This gives only the first occurrence of "abc":

StringPosition["abcdabcdaabcabcd","abc",1]

This shows where both "abc" and "cd" appear. By default, overlaps are included:

StringPosition["abcdabcdcd",{"abc","cd"}]

This does not include overlaps:

StringPosition["abcdabcdcd",{"abc","cd"},Overlaps->False]

Testing for substrings.

This counts occurrences of either substring, by default not including overlaps:

StringCount["abcdabcdcd",{"abc","cd"}]

Replacing substrings according to rules.

This replaces all occurrences of the character a by the string XX:

StringReplace["abcdabcdaabcabcd","a"->"XX"]

This replaces abc by Y, and d by XXX:

StringReplace["abcdabcdaabcabcd",{"abc"->"Y","d"->"XXX"}]

The first occurrence of cde is not replaced because it overlaps with abc:

StringReplace["abcde abacde",{"abc"->"X","cde"->"Y"}]

(RefLink[StringReplace, paclet:ref/StringReplace]) scans a string from left to right, doing all the replacements it can, and then returning the resulting string. Sometimes, however, it is useful to see what all possible single replacements would give. You can get a list of all these results using (RefLink[StringReplaceList, paclet:ref/StringReplaceList]).

This gives a list of the results of replacing each a:

StringReplaceList["aaaaa","a"->"X"]

This shows the results of all possible single replacements:

StringReplaceList["abcde abacde",{"abc"->"X","cde"->"Y"}]

Splitting strings.

This splits the string at every run of spaces:

StringSplit["a b::c d::e f g"]

This splits at each "::":

StringSplit["a b::c d::e f g","::"]

This splits at each colon or space:

StringSplit["a b::c d::e f g",{":"," "}]

Splitting strings with replacements for delimiters.

This inserts {x,y} at each :: delimiter:

StringSplit["a b::c d::e f g","::"->{x,y}]

Sorting strings.

(RefLink[Sort, paclet:ref/Sort]) sorts strings into standard dictionary order:

Sort[{"cat","fish","catfish","Cat"}]

Remove whitespace from ends of string:

StringTrim["   abcabc   "] //FullForm

Find an optimal alignment of two strings:

SequenceAlignment["abcXabcXabc","abcYabcYabc"]