Strings are manipulated using a set of predicates that mirrors the set of predicates used for manipulating atoms. In addition to the list below, string/1 performs the type check for this type and is described in section 4.5.
SWI-Prolog's string primitives are being synchronized with ECLiPSe. We expect the set of predicates documented in this section to be stable, although it might be expanded. In general, SWI-Prolog's text manipulation predicates accept any form of text as input argument - they accept anytext input. anytext comprises:
The predicates produce the type indicated by the predicate name as output. This policy simplifies migration and writing programs that can run unmodified or with minor modifications on systems that do not support strings. Code should avoid relying on this feature as much as possible for clarity as well as to facilitate a more strict mode and/or type checking in future releases.
atom_string("x",'x'). atom_string('x',"x"). atom_string(3.1415,3.1415). atom_string('3r2',3r2). atom_string(3r2,'3r2'). atom_string(6r4,3r2).
+
or -
.+
or -
and the number."1e10"
is a valid number.Unlike other predicates of this family, if instantiated, String cannot be an atom.
The corresponding‘atom-handling' predicate is atom_number/2, with reversed argument order.
quoted(true)
and the result is converted to String.?- term_string(Term, 'a(A)', [variable_names(VNames)]). Term = a(_9674), VNames = ['A'=_9674].
See also: atom_chars/2.
utf8
. All valid stream encodings except for wchar_t
are supported. See section
2.19.1. Note that this translation is only provided for strings.
Creating an atom from bytes requires
atom_string/2.170Strings
are an efficient intermediate and this conversion is needed only in some
uncommon scenarios.'[]'
is ambiguous and
interpreted as an empty string.string_code(-,+,+)
is
deterministic if the searched-for Code appears only once in String.
See also
sub_string/5.String[Index]
notation.A simple split wherever there is a‘.':
?- split_string("a.b.c.d", ".", "", L). L = ["a", "b", "c", "d"].
Consider sequences of separators as a single one:
?- split_string("/home//jan///nice/path", "/", "/", L). L = ["home", "jan", "nice", "path"].
Split and remove white space:
?- split_string("SWI-Prolog, 7.0", ",", " ", L). L = ["SWI-Prolog", "7.0"].
Only remove leading and trailing white space (trim the string):
?- split_string(" SWI-Prolog ", "", "\s\t\n", L). L = ["SWI-Prolog"].
In the typical use cases, SepChars either does not overlap PadChars or is equivalent to handle multiple adjacent separators as a single (often white space). The behaviour with partially overlapping sets of padding and separators should be considered undefined. See also read_string/5.
name_value(String, Name, Value) :- sub_string(String, Before, _, After, "="), !, sub_atom(String, 0, Before, _, Name), sub_string(String, _, After, 0, Value).
atomics_to_string(List,’’, String)
.?- atomics_to_string([gnu, "gnat", 1], ', ', A). A = "gnu, gnat, 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.171Behaviour that is fully compatible would require unlimited look-ahead. Below are some examples:
Read a line:
read_string(Input, "\n", "\r", Sep, String)
Read a line, stripping leading and trailing white space:
read_string(Input, "\n", "\r\t ", Sep, String)
Read up to‘,
’or‘)
’,
unifying Sep with 0',
i.e. Unicode 44, or 0')
,
i.e. Unicode 41:
read_string(Input, ",)", "\t ", Sep, String)
reposition
property (see stream_property/2).
Note that the internal encoding of the data is either ISO Latin 1 or
UTF-8.