MATHEMATICA TUTORIAL

# Operations on Strings

Mathematica 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.

 s1<>s2<>... or StringJoin[{s1,s2,...}] join several strings together StringLength[s] give the number of characters in a string StringReverse[s] reverse the characters in a string

Operations on complete strings.

You can join together any number of strings using .
 Out[1]=
StringLength gives the number of characters in a string.
 Out[2]=
StringReverse reverses the characters in a string.
 Out[3]=
 StringTake[s,n] make a string by taking the first n characters from s StringTake[s,{n}] take the n character from s StringTake[s,{n1,n2}] take characters through StringDrop[s,n] make a string by dropping the first n characters in s StringDrop[s,{n1,n2}] drop characters through

Taking and dropping substrings.

StringTake and StringDrop are the analogs for strings of Take and Drop for lists. Like Take and Drop, they use standard Mathematica 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 .

Here is a sample string.
 Out[4]=
This takes the first five characters from .
 Out[5]=
Here is the fifth character in .
 Out[6]=
This drops the characters 10 through 2, counting from the end of the string.
 Out[7]=
 StringInsert[s,snew,n] insert the string snew at position n in s StringInsert[s,snew,{n1,n2,...}] insert several copies of snew into s

Inserting into a string.

StringInsert[s, snew, n] is set up to produce a string whose n character is the first character of snew.

This produces a new string whose fourth character is the first character of the string .
 Out[8]=
Negative positions are counted from the end of the string.
 Out[9]=
Each copy of is inserted at the specified position in the original string.
 Out[10]=
This uses Riffle to add a space between the words in a list.
 Out[11]=
 StringReplacePart[s,snew,{m,n}] replace the characters at positions m through n in s by the string snew StringReplacePart[s,snew,{{m1,n1},{m2,n2},...}] replace several substrings in s by snew StringReplacePart[s,{snew1,snew2,...},{{m1,n1},{m2,n2},...}] replace substrings in s by the corresponding

Replacing parts of a string.

This replaces characters 2 through 6 by the string .
 Out[12]=
This replaces two runs of characters by the string .
 Out[13]=
Now the two runs of characters are replaced by different strings.
 Out[14]=
 StringPosition[s,sub] give a list of the starting and ending positions at which sub appears as a substring of s StringPosition[s,sub,k] include only the first k occurrences of sub in s StringPosition[s,{sub1,sub2,...}] include occurrences of any of the

Finding positions of substrings.

You can use StringPosition to find where a particular substring appears within a given string. 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 StringTake, StringDrop, and StringReplacePart.

This gives a list of the positions of the substring .
 Out[15]=
This gives only the first occurrence of .
 Out[16]=
This shows where both and appear. By default, overlaps are included.
 Out[17]=
This does not include overlaps.
 Out[18]=
 StringCount[s,sub] count the occurrences of sub in s StringCount[s,{sub1,sub2,...}] count occurrences of any of the StringFreeQ[s,sub] test whether s is free of sub StringFreeQ[s,{sub1,sub2,...}] test whether s is free of all the

Testing for substrings.

This counts occurrences of either substring, by default not including overlaps.
 Out[19]=
 StringReplace[s,sb->sbnew] replace sb by sbnew wherever it appears in s StringReplace[s,{sb1->sbnew1,sb2->sbnew2,...}] replace by the corresponding StringReplace[s,rules,n] do at most n replacements StringReplaceList[s,rules] give a list of the strings obtained by making each possible single replacement StringReplaceList[s,rules,n] give at most n results

Replacing substrings according to rules.

This replaces all occurrences of the character by the string .
 Out[20]=
This replaces by , and by .
 Out[21]=
The first occurrence of is not replaced because it overlaps with .
 Out[22]=

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 StringReplaceList.

This gives a list of the results of replacing each .
 Out[23]=
This shows the results of all possible single replacements.
 Out[24]=
 StringSplit[s] split s into substrings delimited by whitespace StringSplit[s,del] split at delimiter del StringSplit[s,{del1,del2,...}] split at any of the StringSplit[s,del,n] split into at most n substrings

Splitting strings.

This splits the string at every run of spaces.
 Out[25]=
This splits at each .
 Out[26]=
This splits at each colon or space.
 Out[27]=
 StringSplit[s,del->rhs] insert rhs at the position of each delimiter StringSplit[s,{del1->rhs1,del2->rhs2,...}] insert at the position of the corresponding

Splitting strings with replacements for delimiters.

This inserts at each delimiter.
 Out[28]=
 Sort[{s1,s2,s3,...}] sort a list of strings

Sorting strings.

Sort sorts strings into standard dictionary order.
 Out[29]=
 StringTrim[s] trim whitespace from the beginning and end of s StringTrim[s,patt] trim substrings matching patt from the beginning and end
Remove whitespace from ends of string.
 Out[30]//FullForm=
 SequenceAlignment[s1,s2] find an optimal alignment of s1 and s2
Find an optimal alignment of two strings.
 Out[31]=

## Tutorial CollectionTutorial Collection

New to Mathematica? Find your learning path »
Have a question? Ask support »