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.

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 .
In[1]:=
Click for copyable input
Out[1]=
StringLength gives the number of characters in a string.
In[2]:=
Click for copyable input
Out[2]=
StringReverse reverses the characters in a string.
In[3]:=
Click for copyable input
Out[3]=
StringTake[s,n]make a string by taking the first n characters from s
StringTake[s,{n}]take the n^(th) 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 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 .

Here is a sample string.
In[4]:=
Click for copyable input
Out[4]=
This takes the first five characters from .
In[5]:=
Click for copyable input
Out[5]=
Here is the fifth character in .
In[6]:=
Click for copyable input
Out[6]=
This drops the characters 10 through 2, counting from the end of the string.
In[7]:=
Click for copyable input
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^(th) character is the first character of snew.

This produces a new string whose fourth character is the first character of the string .
In[8]:=
Click for copyable input
Out[8]=
Negative positions are counted from the end of the string.
In[9]:=
Click for copyable input
Out[9]=
Each copy of is inserted at the specified position in the original string.
In[10]:=
Click for copyable input
Out[10]=
This uses Riffle to add a space between the words in a list.
In[11]:=
Click for copyable input
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 .
In[12]:=
Click for copyable input
Out[12]=
This replaces two runs of characters by the string .
In[13]:=
Click for copyable input
Out[13]=
Now the two runs of characters are replaced by different strings.
In[14]:=
Click for copyable input
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 .
In[15]:=
Click for copyable input
Out[15]=
This gives only the first occurrence of .
In[16]:=
Click for copyable input
Out[16]=
This shows where both and appear. By default, overlaps are included.
In[17]:=
Click for copyable input
Out[17]=
This does not include overlaps.
In[18]:=
Click for copyable input
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.
In[19]:=
Click for copyable input
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 .
In[20]:=
Click for copyable input
Out[20]=
This replaces by , and by .
In[21]:=
Click for copyable input
Out[21]=
The first occurrence of is not replaced because it overlaps with .
In[22]:=
Click for copyable input
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 .
In[23]:=
Click for copyable input
Out[23]=
This shows the results of all possible single replacements.
In[24]:=
Click for copyable input
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.
In[25]:=
Click for copyable input
Out[25]=
This splits at each .
In[26]:=
Click for copyable input
Out[26]=
This splits at each colon or space.
In[27]:=
Click for copyable input
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.
In[28]:=
Click for copyable input
Out[28]=
Sort[{s1,s2,s3,}]sort a list of strings

Sorting strings.

Sort sorts strings into standard dictionary order.
In[29]:=
Click for copyable input
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.
In[30]:=
Click for copyable input
Out[30]//FullForm=
SequenceAlignment[s1,s2]find an optimal alignment of s1 and s2
Find an optimal alignment of two strings.
In[31]:=
Click for copyable input
Out[31]=