Searching and Reading Strings

Functions like (RefLink[Read, paclet:ref/Read]) and (RefLink[Find, paclet:ref/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 (RefLink[StringToStream, paclet:ref/StringToStream]), which opens an input stream that takes characters not from an external file, but instead from a Wolfram Language string.

Treating strings as input streams.

This opens an input stream for reading from the string:

str=StringToStream["A string of words."]

This reads the first "word" from the string:

Read[str,Word]

This reads the remaining words from the string:

ReadList[str,Word]

This closes the input stream:

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 (RefLink[Read, paclet:ref/Read]). The current position is given as the number of characters from the beginning of the string by the function (RefLink[StreamPosition, paclet:ref/StreamPosition])[stream]. You can explicitly set the current position using (RefLink[SetStreamPosition, paclet:ref/SetStreamPosition])[stream,n].

Here is an input stream associated with a string:

str=StringToStream["123 456 789"]

The current position is initially 0 characters from the beginning of the string:

StreamPosition[str]

This reads a number from the stream:

Read[str,Number]

The current position is now 3 characters from the beginning of the string:

StreamPosition[str]

This sets the current position to be 1 character from the beginning of the string:

SetStreamPosition[str,1]

If you now read a number from the string, you get the 23 part of 123:

Read[str,Number]

This sets the current position to the end of the string:

SetStreamPosition[str,Infinity]

If you now try to read from the stream, you will always get (RefLink[EndOfFile, paclet:ref/EndOfFile]):

Read[str,Number]

This closes the stream:

Close[str]

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 (RefLink[StringToStream, paclet:ref/StringToStream]) to allow further processing of these strings within the Wolfram Language. Once you have created an input stream using (RefLink[StringToStream, paclet:ref/StringToStream]), you can read and search the string using any of the functions discussed for files.

This puts the whole contents of textfile into a string:

s=First[ReadList["ExampleData/textfile",Record,RecordSeparators->{}]]

This opens an input stream for the string:

str=StringToStream[s]

This gives the lines of text in the string that contain is:

FindList[str,"is"]

This resets the current position back to the beginning of the string:

SetStreamPosition[str,0]

This finds the first occurrence of the in the string, and leaves the current point just after it:

Find[str,"the",RecordSeparators->{" "}]

This reads the "word" which appears immediately after the:

Read[str,Word]

This closes the input stream:

Close[str]