Searching and Reading Strings

Functions like Read and Find are most often used for processing text and data from external files. In some cases, however, you may find it convenient to use these same functions to process strings within the Wolfram Language. You can do this by using the function StringToStream, which opens an input stream that takes characters not from an external file, but instead from a Wolfram Language string.

StringToStream["string"]open an input stream for reading from a string
Close[stream]close an input stream

Treating strings as input streams.

This opens an input stream for reading from the string.
In[1]:=
Click for copyable input
Out[1]=
This reads the first "word" from the string.
In[2]:=
Click for copyable input
Out[2]=
This reads the remaining words from the string.
In[3]:=
Click for copyable input
Out[3]=
This closes the input stream.
In[4]:=
Click for copyable input
Out[4]=

Input streams associated with strings work just like those with files. At any given time, there is a current position in the stream, which advances when you use functions like Read. The current position is given as the number of characters from the beginning of the string by the function StreamPosition[stream]. You can explicitly set the current position using SetStreamPosition[stream,n].

Here is an input stream associated with a string.
In[5]:=
Click for copyable input
Out[5]=
The current position is initially 0 characters from the beginning of the string.
In[6]:=
Click for copyable input
Out[6]=
This reads a number from the stream.
In[7]:=
Click for copyable input
Out[7]=
The current position is now 3 characters from the beginning of the string.
In[8]:=
Click for copyable input
Out[8]=
This sets the current position to be 1 character from the beginning of the string.
In[9]:=
Click for copyable input
Out[9]=
If you now read a number from the string, you get the part of .
In[10]:=
Click for copyable input
Out[10]=
This sets the current position to the end of the string.
In[11]:=
Click for copyable input
Out[11]=
If you now try to read from the stream, you will always get EndOfFile.
In[12]:=
Click for copyable input
Out[12]=
This closes the stream.
In[13]:=
Click for copyable input
Out[13]=

Particularly when you are processing large volumes of textual data, it is common to read fairly long strings into the Wolfram Language, then to use StringToStream to allow further processing of these strings within the Wolfram Language. Once you have created an input stream using StringToStream, you can read and search the string using any of the functions discussed for files.

This puts the whole contents of into a string.
In[14]:=
Click for copyable input
Out[14]=
This opens an input stream for the string.
In[15]:=
Click for copyable input
Out[15]=
This gives the lines of text in the string that contain .
In[16]:=
Click for copyable input
Out[16]=
This resets the current position back to the beginning of the string.
In[17]:=
Click for copyable input
Out[17]=
This finds the first occurrence of in the string, and leaves the current point just after it.
In[18]:=
Click for copyable input
Out[18]=
This reads the "word" which appears immediately after .
In[19]:=
Click for copyable input
Out[19]=
This closes the input stream.
In[20]:=
Click for copyable input
Out[20]=