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

s_{1}<>s_{2}<>… or StringJoin[{s_{1},s_{2},…}] | 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.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

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,{n_{1},n_{2}}] | take characters through |

StringDrop[s,n] | make a string by dropping the first n characters in s |

StringDrop[s,{n_{1},n_{2}}] | 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 .

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

StringInsert[s,snew,n] | insert the string snew at position n in s |

StringInsert[s,snew,{n_{1},n_{2},…}] | insert several copies of snew into s |

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

In[8]:= |

Out[8]= |

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

Out[11]= |

StringReplacePart[s,snew,{m,n}] | replace the characters at positions m through n in s by the string snew |

StringReplacePart[s,snew,{{m_{1},n_{1}},{m_{2},n_{2}},…}] | replace several substrings in s by snew |

StringReplacePart[s,{snew_{1},snew_{2},…},{{m_{1},n_{1}},{m_{2},n_{2}},…}] | replace substrings in s by the corresponding |

In[12]:= |

Out[12]= |

In[13]:= |

Out[13]= |

In[14]:= |

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,{sub_{1},sub_{2},…}] | 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.

In[15]:= |

Out[15]= |

In[16]:= |

Out[16]= |

In[17]:= |

Out[17]= |

In[18]:= |

Out[18]= |

StringCount[s,sub] | count the occurrences of sub in s |

StringCount[s,{sub_{1},sub_{2},…}] | count occurrences of any of the |

StringFreeQ[s,sub] | test whether s is free of sub |

StringFreeQ[s,{sub_{1},sub_{2},…}] | test whether s is free of all the |

In[19]:= |

Out[19]= |

StringReplace[s,sb->sbnew] | replace sb by sbnew wherever it appears in s |

StringReplace[s,{sb_{1}->sbnew_{1},sb_{2}->sbnew_{2},…}] | 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.

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

In[22]:= |

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.

In[23]:= |

Out[23]= |

In[24]:= |

Out[24]= |

StringSplit[s] | split s into substrings delimited by whitespace |

StringSplit[s,del] | split at delimiter del |

StringSplit[s,{del_{1},del_{2},…}] | split at any of the |

StringSplit[s,del,n] | split into at most n substrings |

In[25]:= |

Out[25]= |

In[26]:= |

Out[26]= |

In[27]:= |

Out[27]= |

StringSplit[s,del->rhs] | insert rhs at the position of each delimiter |

StringSplit[s,{del_{1}->rhs_{1},del_{2}->rhs_{2},…}] | insert at the position of the corresponding |

Splitting strings with replacements for delimiters.

In[28]:= |

Out[28]= |

Sort[{s_{1},s_{2},s_{3},…}] | sort a list of strings |

In[29]:= |

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 |

In[30]:= |

Out[30]//FullForm= | |

SequenceAlignment[s_{1},s_{2}] | find an optimal alignment of s_{1} and s_{2} |

In[31]:= |

Out[31]= |