Wolfram ResearchProductsPurchasingServices & ResourcesAbout UsOur Sites
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Previous section-----Next section

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.

 <>  <> ... or StringJoin[{ ,  , ... }]
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]:=  "aaaaaaa" <> "bbb" <> "cccccccccc"

Out[1]=

StringLength gives the number of characters in a string.

In[2]:=  StringLength[%]

Out[2]=

StringReverse reverses the characters in a string.

In[3]:=  StringReverse["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, { ,  }] take characters  through
StringDrop[s, n] make a string by dropping the first n characters in s
StringDrop[s, { ,  }] 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 1.

Here is a sample string.

In[4]:=  alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Out[4]=

This takes the first five characters from alpha.

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

Out[5]=

Here is the fifth character in alpha.

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

Out[6]=

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

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

Out[7]=

StringInsert[s, snew, n] insert the string snew at position n in s
StringInsert[s, snew, { ,  , ... }]
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 "XX".

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

Out[8]=

Negative positions are counted from the end of the string.

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

Out[9]=

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

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

Out[10]=

StringReplacePart[s, snew, {m, n}]
replace the characters at positions m through n in s by the string snew
StringReplacePart[s, snew, {{ ,  }, { ,  }, ... }]
replace several substrings in s by snew
StringReplacePart[s, { ,  , ... }, {{ ,  }, { ,  }, ... }]
replace substrings in s by the corresponding

Replacing parts of a string.
This replaces characters 2 through 6 by the string "XXX".

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

Out[11]=

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

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

Out[12]=

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

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

Out[13]=

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, { ,  , ... }]
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 "abc".

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

Out[14]=

This gives only the first occurrence of "abc".

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

Out[15]=

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

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

Out[16]=

This does not include overlaps.

In[17]:=  StringPosition["abcdabcdcd", {"abc", "cd"},
Overlaps -> False]

Out[17]=

StringCount[s, sub] count the occurrences of sub in s
StringCount[s, { ,  , ... }] count occurrences of any of the
StringFreeQ[s, sub] test whether s is free of sub
StringFreeQ[s, { ,  , ... }] test whether s is free of all the

Testing for substrings.
This counts occurrences of either substring, by default not including overlaps.

In[18]:=  StringCount["abcdabcdcd", {"abc", "cd"}]

Out[18]=

StringReplace[s, sb -> sbnew] replace sb by sbnew wherever it appears in s
StringReplace[s, { ->  ,  ->  , ... }]
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 a by the string XX.

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

Out[19]=

This replaces abc by Y, and d by XXX.

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

Out[20]=

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

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

Out[21]=

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 of the a's.

In[22]:=  StringReplaceList["aaaaa", "a" -> "X"]

Out[22]=

This shows the results of all possible single replacements.

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

Out[23]=

StringSplit[s] split s into substrings delimited by whitespace
StringSplit[s, del] split at delimiter del
StringSplit[s, { ,  , ... }] 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[24]:=  StringSplit["a b::c d::e f g"]

Out[24]=

This splits at each "::".

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

Out[25]=

This splits at each colon or space.

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

Out[26]=

StringSplit[s, del -> rhs] insert rhs at the position of each delimiter
StringSplit[s, { ->  ,  ->  , ... }]
insert  at the position of the corresponding

Splitting strings with replacements for delimiters.
This inserts {x, y} at each :: delimiter.

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

Out[27]=

Sort[{ ,  ,  , ... }] sort a list of strings

Sorting strings.
Sort sorts strings into standard dictionary order.

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

Out[28]=


Any questions about topics on this page? Click here to get an individual response.Buy NowFree TrialMore Information



 © 2008 Wolfram Research, Inc.  Terms of Use  Privacy Policy |
Sign up for our newsletter: