5.2.1 Predicates that operate on strings
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • SWI-Prolog extensions
        • The string type and its double quoted syntax
          • Predicates that operate on strings
            • atom_string/2
            • number_string/2
            • term_string/2
            • term_string/3
            • string_chars/2
            • string_codes/2
            • text_to_string/2
            • string_length/2
            • string_code/3
            • get_string_code/3
            • string_concat/3
            • split_string/4
            • sub_string/5
            • atomics_to_string/2
            • atomics_to_string/3
            • string_upper/2
            • string_lower/2
            • read_string/3
            • read_string/5
            • open_string/2
    • Packages
Availability:built-in
read_string(+Stream, +SepChars, +PadChars, -Sep, -String)
Read a string from Stream, providing functionality similar to split_string/4. The predicate performs the following steps:

  1. Skip all characters that match PadChars
  2. Read up to a character that matches SepChars or end of file
  3. Discard trailing characters that match PadChars from the collected input
  4. Unify String with a string created from the input and Sep with the separator character read. If input was terminated by the end of the input, Sep is unified with -1.

The predicate read_string/5 called repeatedly on an input until Sep is -1 (end of file) is equivalent to reading the entire file into a string and calling split_string/4, provided that SepChars and PadChars are not partially overlapping.150Behaviour that is fully compatible would requite unlimited look-ahead. Below are some examples:

% Read a line
read_string(Input, "\n", "\r", End, String)
% Read a line, stripping leading and trailing white space
read_string(Input, "\n", "\r\t ", End, String)
% Read upto , or ), unifying End with 0', or 0')
read_string(Input, ",)", "\t ", End, String)