Function Repository Resource:

StringSplitAfter

Source Notebook

Split a string after each substring matching a string pattern

Contributed by: Ian Ford (Wolfram Research)

ResourceFunction["StringSplitAfter"]["string"]

splits "string" into a list of substrings after each whitespace.

ResourceFunction["StringSplitAfter"]["string",patt]

splits "string" into substrings after delimiters matching the string expression patt.

ResourceFunction["StringSplitAfter"]["string",{p1,p2,}]

splits "string" after any of the pi.

ResourceFunction["StringSplitAfter"]["string",pattval]

replaces each delimiter matching patt with val.

ResourceFunction["StringSplitAfter"]["string",{p1v1,}]

replaces each delimiter matching pi with vi.

ResourceFunction["StringSplitAfter"][{s1,s2,},p]

gives the list of results for each of the si.

Details and Options

Unlike StringSplit, ResourceFunction["StringSplitAfter"] will not remove the delimiters—it will only split the string after each delimiter.
ResourceFunction["StringSplitAfter"][s,p] otherwise behaves like StringSplit[s,p,All].
ResourceFunction["StringSplitAfter"] takes the same options as StringSplit.

Examples

Basic Examples (2) 

Split a string after each whitespace (without removing the whitespace):

In[1]:=
ResourceFunction["StringSplitAfter"]["a bbb  cccc aa   d"]
Out[1]=

Split a string after every "--" (without removing the "--"):

In[2]:=
ResourceFunction["StringSplitAfter"]["a--bbb---ccc--dddd", "--"]
Out[2]=

Scope (7) 

Split after every run of spaces:

In[3]:=
ResourceFunction["StringSplitAfter"]["the cat in the hat"]
Out[3]=

Use string patterns:

In[4]:=
ResourceFunction["StringSplitAfter"]["192.168.0.1", "."]
Out[4]=
In[5]:=
ResourceFunction["StringSplitAfter"]["123  2.3  4  6", WhitespaceCharacter ..]
Out[5]=
In[6]:=
ResourceFunction["StringSplitAfter"]["11a22b3", _?LetterQ]
Out[6]=

Regular expressions:

In[7]:=
ResourceFunction[
 "StringSplitAfter"]["A tree, an apple, four pears. And more: two sacks", RegularExpression["\\W+"]]
Out[7]=

Mixed regular expressions and string patterns:

In[8]:=
ResourceFunction[
 "StringSplitAfter"]["primes: 2 two 3 three 5 five ...", Whitespace ~~ RegularExpression["\\d"] ~~ Whitespace]
Out[8]=

Split into substrings after either delimiter:

In[9]:=
ResourceFunction["StringSplitAfter"]["a-b:c-d:e-f-g", {":", "-"}]
Out[9]=
In[10]:=
ResourceFunction["StringSplitAfter"]["a-b:c-d:e-f-g", ":" | "-"]
Out[10]=

Replace a delimiter:

In[11]:=
ResourceFunction["StringSplitAfter"]["a b::c d::e f g", "::" -> "--"]
Out[11]=

SplitStringAfter automatically threads over lists of strings:

In[12]:=
ResourceFunction[
 "StringSplitAfter"][{"a:b:c:d", "listable:element"}, ":"]
Out[12]=

Options (1) 

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

In[13]:=
ResourceFunction["StringSplitAfter"]["cat Cat hat CAT", "c", IgnoreCase -> True]
Out[13]=

Version History

  • 1.0.0 – 25 September 2019

Related Resources

License Information