This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)

Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Strings and Characters /

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

Operations on complete strings.

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

In[1]:= "aaaaaaa" <> "bbb" <> "cccccccccc"


StringLength gives the number of characters in a string.

In[2]:= StringLength[%]


StringReverse reverses the characters in a string.

In[3]:= StringReverse["A string."]


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

Here is a sample string.



This takes the first five characters from alpha.

In[5]:= StringTake[alpha, 5]


Here is the fifth character in alpha.

In[6]:= StringTake[alpha, {5}]


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

In[7]:= StringDrop[alpha, {-10, -2}]


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 "XX".

In[8]:= StringInsert["abcdefgh", "XX", 4]


Negative positions are counted from the end of the string.

In[9]:= StringInsert["abcdefgh", "XXX", -1]


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

In[10]:= StringInsert["abcdefgh", "XXX", {2, 4, -1}]


Replacing parts of a string.

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

In[11]:= StringReplacePart["abcdefgh", "XXX", {2, 6}]


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

In[12]:= StringReplacePart["abcdefgh", "XXX", {{2, 3}, {5, -1}}]


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

In[13]:= StringReplacePart["abcdefgh", {"XXX", "YYYY"},
{{2, 3}, {5, -1}}]


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 "abc".

In[14]:= StringPosition["abcdabcdaabcabcd", "abc"]


This gives only the first occurrence of "abc".

In[15]:= StringPosition["abcdabcdaabcabcd", "abc", 1]


This shows where both "abc" and "cd" appear. Overlaps between these strings are taken into account.

In[16]:= StringPosition["abcdabcdaabcabcd", {"abc", "cd"}]


Replacing substrings according to rules.

StringReplace allows you to perform replacements for substrings within a string. StringReplace sequentially goes through a string, testing substrings that start at each successive character position. To each substring, it tries in turn each of the transformation rules you have specified. If any of the rules apply, it replaces the substring, then continues to go through the string, starting at the character position after the end of the substring.

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

In[17]:= StringReplace["abcdabcdaabcabcd", "a" -> "XX"]


This replaces abc by Y, and d by XXX.

In[18]:= StringReplace["abcdabcdaabcabcd",
{"abc" -> "Y", "d" -> "XXX"}]


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

In[19]:= StringReplace["abcde abacde",
{"abc" -> "X", "cde" -> "Y"}]


Case-independent operations.

This replaces all occurrences of "the", independent of case.

In[20]:= StringReplace["The cat in the hat.", "the" -> "a",
IgnoreCase -> True]


Sorting strings.

Sort sorts strings into standard dictionary order.

In[21]:= Sort[{"cat", "fish", "catfish", "Cat"}]