Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > String Manipulation > String Operations >

StringSplit

StringSplit["string"]
splits "string" into a list of substrings separated by whitespace.
StringSplit["string", patt]
splits into substrings separated by delimiters matching the string expression patt.
StringSplit["string", {p1, p2, ...}]
splits at any of the pi.
StringSplit["string", patt->val]
inserts val at the position of each delimiter.
StringSplit["string", {p1->v1, ...}]
inserts vi at the position of each delimiter pi.
StringSplit["string", patt, n]
splits into at most n substrings.
StringSplit[{s1, s2, ...}, p]
gives the list of results for each of the si.
  • StringSplit[s] does not return the whitespace characters that delimit the substrings it returns.
  • Whitespace includes any number of spaces, tabs and newlines.
  • The string expression patt can contain any of the objects specified in the notes for StringExpression.
  • If s contains two adjacent delimiters, StringSplit considers there to be a zero-length substring "" between them.
  • StringSplit[s, patt] by default gives the list of substrings of s that occur between delimiters defined by patt; it does not include the delimiters themselves.
  • StringSplit[s, patt->val] includes val at the position of each delimiter.
  • StringSplit[s, patt:>val] evaluates val only when the pattern is found.
  • StringSplit["string", {p1->v1, ..., pa, ...}] includes v1 at the position of delimiters matching p1, but omits delimiters matching pa.
  • By default, StringSplit[s, patt] drops zero-length substrings associated with delimiters that appear at the beginning or end of s.
  • StringSplit[s, patt, All] returns all substrings, including zero-length ones at the beginning or end.
Pick out substrings delimited by whitespace:
Show the substrings with quotes:
Split a string at every --:
Pick out substrings delimited by whitespace:
In[1]:=
Click for copyable input
Out[1]=
Show the substrings with quotes:
In[2]:=
Click for copyable input
Out[2]//InputForm=
 
Split a string at every --:
In[1]:=
Click for copyable input
Out[1]=
Split at every run of spaces:
Use string patterns:
Regular expressions:
Mixed regular expressions and string patterns:
Split into substrings separated by either delimiter:
Insert a value at the position of a delimiter:
Include the delimiters in the output:
StringSplit automatically threads over lists of strings:
All substrings, including zero-length ones at the beginning or end:
Split a string at every "c", including uppercase letters:
A base random DNA string:
Sequences with adenine symmetrically placed:
Text analysis with some right and left context:
Use StringSplit to find all occurrences of the word "power":
Compute part of the left and right contexts in which each word occurs:
List extensions of files in a directory and its subdirectories:
Splitting at whitespace is equivalent to cases of non-whitespace sequences:
StringSplit with a rule is equivalent to StringReplace:
A null delimiter splits at every character:
Using StringSplit on a comma-separated values string:
In many cases Import and ImportString provide direct functionality:
StringSplit by default splits only at whitespace:
This splits into words:
Another way to split into words:
New in 5.1
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team