ReadString¶
Read the contents of a file or stream as a string.
Reading a stream takes everything left in it:
A second argument gives a terminator, so repeated reads yield the fields between separators:
A stream with nothing left gives EndOfFile:
The terminator may be a string pattern instead of a literal string. The read returns the text before the first match:
$ wo 'ReadString[StringToStream["aaa%--%HEAD%--%rest"], "%" ~~ Repeated["-", {1, 10}] ~~ "%HEAD%" ~~ Repeated["-", {1, 10}] ~~ "%"]'
aaa
An anchor or a repeat on its own is not a terminator, though the same construct inside an alternative is:
Nothing matching gives everything that is left, and a message:
$ wo 'ReadString[StringToStream["abc"], StartOfLine ~~ "zzz"]'
ReadString::notfound: Specified terminator not found.
abc
Option rules may follow the terminator: