Bi-directional conversion between an atom and a string. At least one of
the two arguments must be instantiated. An initially uninstantiated
variable on the “string side'' is always instantiated to a string.
An initially uninstantiated variable on the “atom side'' is always
instantiated to an atom. If both arguments are instantiated, their
list-of-character representations must match, but the types are not
enforced. The following all succeed:
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).
Bi-directional conversion between a number and a string. At least one of
the two arguments must be instantiated. Besides the type used to
represent the text, this predicate differs in several ways from its ISO
cousin:169Note that SWI-Prolog's
syntax for numbers is not ISO compatible either.
- If String does not represent a number, the predicate fails
rather than throwing a syntax error exception.
- Leading white space and Prolog comments are not allowed.
- Numbers may start with
+
or -
.
- It is not allowed to have white space between a leading
+
or -
and the number.
- Floating point numbers in exponential notation do not require a dot
before exponent, i.e.,
"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.
As term_string/2,
passing Options to either read_term/2
or write_term/2.
For example:
?- term_string(Term, 'a(A)', [variable_names(VNames)]).
Term = a(_9674),
VNames = ['A'=_9674].
Break String into SubStrings. The SepChars
argument provides the characters that act as separators and thus the
length of
SubStrings is one more than the number of separators found if
SepChars and PadChars do not have common
characters. If
SepChars and PadChars are equal, sequences of
adjacent separators act as a single separator. Leading and trailing
characters for each substring that appear in PadChars are
removed from the substring. The input arguments can be either atoms,
strings or char/code lists. Compatible with ECLiPSe. Below are some
examples:
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.
This predicate is functionally equivalent to sub_atom/5,
but operates on strings. Note that this implies the string input
arguments can be either strings or atoms. If SubString is
unbound (output) it is unified with a string. The following example
splits a string of the form
<name>=<value> into the name part (an
atom) and the value (a string).
name_value(String, Name, Value) :-
sub_string(String, Before, _, After, "="),
!,
sub_atom(String, 0, Before, _, Name),
sub_string(String, _, After, 0, Value).
Creates a string just like atomics_to_string/2,
but inserts
Separator between each pair of inputs. For example:
?- atomics_to_string([gnu, "gnat", 1], ', ', A).
A = "gnu, gnat, 1"
Read a string from Stream, providing functionality similar to
split_string/4.
The predicate performs the following steps:
- Skip all characters that match PadChars
- Read up to a character that matches SepChars or end of
file
- Discard trailing characters that match PadChars from the
collected input
- Unify String with a string created from the input and
Sep with the code of 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.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)