|Did you know ...||Search Documentation:|
|Predicates that operate on strings|
Strings may be manipulated by a set of predicates that is similar to the manipulation of atoms. In addition to the list below, string/1 performs the type check for this type and is described in section 4.6.
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 and 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.
"1e10"is a valid number.
quoted(true)and the result is converted to String.
?- term_string(Term, 'a(A)', [variable_names(VNames)]). Term = a(_G1466), VNames = ['A'=_G1466].
''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.
% a simple split ?- 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 ?- 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_string(String, 0, Before, _, NameString), atom_string(Name, NameString), 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.154Behaviour 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)