StringExtract

StringExtract["string",n]

extracts the n^(th) block of characters in string, where blocks of characters are defined as delimited by whitespace.

StringExtract["string",{pos1,pos2,}]

extracts blocks at several positions in string.

StringExtract["string",seppos]

takes blocks to be delimited by separators that match sep.

StringExtract["string",pos1,pos2,]

extracts blocks at positions posi, delimiting with whitespace for the lowest level, newlines for the next level, and a successively increasing number of newlines thereafter.

StringExtract["string",sep1pos1,sep2pos2,]

gives a nested list of blocks, with the sepi used as separators for successive levels.

Details

  • The position specifications for blocks can have any of the following forms:
  • nn^(th) block
    -nn^(th) block counting from the end
    {n1,n2,}collection of blocks
    n1;;n2n1^(th) through n2^(th) block
    Allall blocks
  • The separator sep can be a literal string or a string pattern.

Examples

open allclose all

Basic Examples  (4)

Take the second whitespace-delimited element in a string:

In[1]:=
Click for copyable input
Out[1]=

Take a list of elements:

In[2]:=
Click for copyable input
Out[2]=

Take the first two whitespace-delimited elements:

In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input

Take the last whitespace-delimited element:

In[5]:=
Click for copyable input
Out[5]=

Take the second colon-delimited element in a string:

In[1]:=
Click for copyable input
Out[1]=

Give all the colon-delimited elements:

In[2]:=
Click for copyable input
Out[2]=

Take the first whitespace-delimited word from each line:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Take the first word from the 3rd line:

In[3]:=
Click for copyable input
Out[3]=

Take every word from the third line:

In[4]:=
Click for copyable input
Out[4]=

Elements not present in a group yield Missing[]:

In[5]:=
Click for copyable input
Out[5]=

Extract the first column from a string in CSV (comma-separated values) format:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

Extract all columns:

In[3]:=
Click for copyable input
Out[3]=

Extract only the first row:

In[4]:=
Click for copyable input
Out[4]=

Properties & Relations  (1)

See Also

StringSplit  ImportString  StringTake  StringPosition  StringPartition  DelimitedSequence  StringRiffle

Introduced in 2015
(10.1)