({{, (RefLink[StringSplit, paclet:ref/StringSplit])["string"]
splits "string" into a list of substrings separated by whitespace. }, {, (RefLink[StringSplit, paclet:ref/StringSplit])["string",patt]
splits into substrings separated by delimiters matching the string expression patt. }, {, (RefLink[StringSplit, paclet:ref/StringSplit])["string",{(Subscript[p, 1]),(Subscript[p, 2]),…}]
splits at any of the (Subscript[p, i]). }, {, (RefLink[StringSplit, paclet:ref/StringSplit])["string",patt->val]
inserts val at the position of each delimiter. }, {, (RefLink[StringSplit, paclet:ref/StringSplit])["string",{(Subscript[p, 1])->(Subscript[v, 1]),…}]
inserts (Subscript[v, i]) at the position of each delimiter (Subscript[p, i]). }, {, (RefLink[StringSplit, paclet:ref/StringSplit])["string",patt,n]
splits into at most n substrings. }, {, (RefLink[StringSplit, paclet:ref/StringSplit])[{(Subscript[s, 1]),(Subscript[s, 2]),…},p]
gives the list of results for each of the (Subscript[s, i]). }})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[StringSplit, paclet:ref/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 (RefLink[StringExpression, paclet:ref/StringExpression]).

(RefLink[StringSplit, paclet:ref/StringSplit])[s] is equivalent to (RefLink[StringSplit, paclet:ref/StringSplit])[s,(RefLink[Whitespace, paclet:ref/Whitespace])].

If s contains two adjacent delimiters, (RefLink[StringSplit, paclet:ref/StringSplit]) considers there to be a zero-length substring "" between them.

(RefLink[StringSplit, paclet:ref/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.

(RefLink[StringSplit, paclet:ref/StringSplit])[s,patt->val] includes val at the position of each delimiter.

(RefLink[StringSplit, paclet:ref/StringSplit])[s,patt:>val] evaluates val only when the pattern is found.

(RefLink[StringSplit, paclet:ref/StringSplit])["string",{(Subscript[p, 1])->(Subscript[v, 1]),…,(Subscript[p, a]),…}] includes (Subscript[v, 1]) at the position of delimiters matching (Subscript[p, 1]), but omits delimiters matching (Subscript[p, a]).

By default, (RefLink[StringSplit, paclet:ref/StringSplit])[s,patt] drops zero-length substrings associated with delimiters that appear at the beginning or end of s.

(RefLink[StringSplit, paclet:ref/StringSplit])[s,patt,(RefLink[All, paclet:ref/All])] returns all substrings, including zero-length ones at the beginning or end.

Setting the option (RefLink[IgnoreCase, paclet:ref/IgnoreCase])->(RefLink[True, paclet:ref/True]) makes (RefLink[StringSplit, paclet:ref/StringSplit]) treat lowercase and uppercase letters as equivalent.

(RefLink[StringSplit, paclet:ref/StringSplit])["string",(RefLink[RegularExpression, paclet:ref/RegularExpression])["regex"]] splits at delimiters matching the specified regular expression.

OpenersPrimaryExamplesSection(Spacer1)Examples  (21)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Pick out substrings delimited by whitespace:

StringSplit["a bbb  cccc aa   d"]

Show the substrings with quotes:

InputForm[%]

Split a string at every --:

StringSplit["a--bbb---ccc--dddd","--"]

OpenersExampleSection1(Spacer1)Scope  (8)

Split at every run of spaces:

StringSplit["the cat in the hat"]

Use string patterns:

StringSplit["192.168.0.1","."]

StringSplit["123  2.3  4  6",WhitespaceCharacter..]

StringSplit["11a22b3",_?LetterQ]

Regular expressions:

StringSplit["A tree, an apple, four pears. And more: two sacks",RegularExpression["\\W+"]]

Mixed regular expressions and string patterns:

StringSplit["primes: 2 two 3 three 5 five ...",Whitespace~~RegularExpression["\\d"]~~Whitespace]

Split into substrings separated by either delimiter:

StringSplit["a-b:c-d:e-f-g",{":","-"}]

StringSplit["a-b:c-d:e-f-g", ":"|"-"]

Insert a value at the position of a delimiter:

StringSplit["a b::c d::e f g","::"->"--"]

Include the delimiters in the output:

StringSplit["a--b c--d e",x:"--":>x]

(RefLink[StringSplit, paclet:ref/StringSplit]) automatically threads over lists of strings:

StringSplit[{"a:b:c:d","listable:element"},":"]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

All substrings, including zero-length ones at the beginning or end:

StringSplit[":a:b:c:",":",All]

InputForm[%]

OpenersExampleSection3(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)IgnoreCase  (1)

Split a string at every "c", including uppercase letters:

StringSplit["cat Cat hat CAT","c",IgnoreCase->True]

OpenersExampleSection4(Spacer1)Applications  (4)

Make a nested array by applying (RefLink[StringSplit, paclet:ref/StringSplit]) twice:

StringSplit[StringSplit["11:12:13//21:22:23//31:32:33","//"],":"]

A base random DNA string:

SeedRandom[1234];
dna = StringJoin[RandomChoice[{"a","c","g","t"},500]];

Sequences with adenine symmetrically placed:

StringSplit[dna,x:("a"~~y_~~_~~y_~~"a"):>Style[x,FontColor->Red]] //Row

Text analysis with some right and left context:

text = Import["ExampleData/USConstitution.txt"];

Use (RefLink[StringSplit, paclet:ref/StringSplit]) to find all occurrences of the word "power":

a = StringSplit[text, "power",10];

Compute part of the left and right contexts in which each word occurs:

Apply[Row[{"…",StringTake[#1,-Min[15,StringLength[#1]]],Style["power",Blue],StringTake[#2,Min[15,StringLength[#2]]],"…"}]&,Partition[a,2,1],{1}]//Column

List extensions of files in a directory and its subdirectories:

SetDirectory[$InstallationDirectory];

files =FileNames["@.*",{"*"},Infinity];

Union[Last[StringSplit[#,"."]] & /@ files]

OpenersExampleSection5(Spacer1)Properties & Relations  (4)

Splitting at whitespace is equivalent to cases of non-whitespace sequences:

StringSplit["a bbb  cccc aa   d"]

StringCases["a bbb  cccc aa   d",Except[WhitespaceCharacter]..]

(RefLink[StringSplit, paclet:ref/StringSplit]) with a rule is equivalent to (RefLink[StringReplace, paclet:ref/StringReplace]):

StringJoin[StringSplit["ab::c::d::ef","::"->"X"]]

StringReplace["ab::c::d::ef","::"->"X"]

A null delimiter splits at every character:

StringSplit["abcdefg",""]

Characters["abcdefg"]

Using (RefLink[StringSplit, paclet:ref/StringSplit]) on a comma-separated values string:

StringSplit[StringSplit["123,  2.3, 45.\n345, 1.4, 450","\n"],","]//ToExpression

In many cases (RefLink[Import, paclet:ref/Import]) and (RefLink[ImportString, paclet:ref/ImportString]) provide direct functionality:

ImportString["123,  2.3, 45.\n345, 1.4, 450","CSV"]

OpenersExampleSection6(Spacer1)Possible Issues  (1)

(RefLink[StringSplit, paclet:ref/StringSplit]) by default splits only at whitespace:

StringSplit["This is a sentence--which goes on."]

This splits into words:

StringSplit["This is a sentence, which goes on.",Except[WordCharacter]..]

Another way to split into words:

StringCases["This is a sentence, which goes on.",WordCharacter..]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[SequenceSplit, paclet:ref/SequenceSplit])  ▪  (RefLink[Split, paclet:ref/Split])  ▪  (RefLink[StringTrim, paclet:ref/StringTrim])  ▪  (RefLink[StringCases, paclet:ref/StringCases])  ▪  (RefLink[FileNameSplit, paclet:ref/FileNameSplit])  ▪  (RefLink[StringExtract, paclet:ref/StringExtract])  ▪  (RefLink[StringPartition, paclet:ref/StringPartition])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Operations on Strings, paclet:tutorial/OperationsOnStrings])}, {▪(RefLinkPlain[Special Characters, paclet:tutorial/SpecialCharacters-StringsAndCharacters])}, {▪(RefLinkPlain[String Patterns, paclet:tutorial/StringPatterns])}, {▪(RefLinkPlain[Working with String Patterns, paclet:tutorial/WorkingWithStringPatternsOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[String Operations, paclet:guide/StringOperations])}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[Text Normalization, paclet:guide/TextNormalization])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[Linguistic Data, paclet:guide/LinguisticData])}, {▪(RefLinkPlain[Scientific Data Analysis, paclet:guide/ScientificDataAnalysis])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])(WebLink[: String Patterns and Templates , https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])}})}})