This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Files and Streams  /

2.11.9 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 Mathematica. 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 Mathematica string.

Treating strings as input streams.

  • This opens an input stream for reading from the string.
  • In[1]:= str = StringToStream["A string of words."]


  • This reads the first "word" from the string.
  • In[2]:= Read[str, Word]


  • This reads the remaining words from the string.
  • In[3]:= ReadList[str, Word]


  • This closes the input stream.
  • In[4]:= Close[str]


    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 bytes 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]:= str = StringToStream["123 456 789"]


  • The current position is initially 0 bytes from the beginning of the string.
  • In[6]:= StreamPosition[str]


  • This reads a number from the stream.
  • In[7]:= Read[str, Number]


  • The current position is now 3 bytes from the beginning of the string.
  • In[8]:= StreamPosition[str]


  • This sets the current position to be 1 byte from the beginning of the string.
  • In[9]:= SetStreamPosition[str, 1]


  • If you now read a number from the string, you get the 23 part of 123.
  • In[10]:= Read[str, Number]


  • This sets the current position to the end of the string.
  • In[11]:= SetStreamPosition[str, Infinity]


  • If you now try to read from the stream, you will always get EndOfFile.
  • In[12]:= Read[str, Number]


  • This closes the stream.
  • In[13]:= Close[str]


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

  • This puts the whole contents of textfile into a string.
  • In[14]:= s = First[ ReadList["textfile", Record,
    RecordSeparators -> {}] ]


  • This opens an input stream for the string.
  • In[15]:= str = StringToStream[s]


  • This gives the lines of text in the string that contain is.
  • In[16]:= FindList[str, "is"]


  • This resets the current position back to the beginning of the string.
  • In[17]:= SetStreamPosition[str, 0]


  • This finds the first occurrence of the in the string, and leaves the current point just after it.
  • In[18]:= Find[str, "the", RecordSeparators -> {" "}]


  • This reads the "word" which appears immediately after the.
  • In[19]:= Read[str, Word]


  • This closes the input stream.
  • In[20]:= Close[str]