SWI-Prolog supports the data type string. Strings are a
time and space efficient mechanism to handle text in Prolog. Strings are
stored as a byte array on the global (term) stack and thus destroyed on
backtracking and reclaimed by the garbage collector.
Strings were added to SWI-Prolog based on an early draft of the ISO
standard, offerring a mechanism to represent temporary character data
efficiently. As SWI-Prolog strings can handle 0-bytes, they are
frequently used through the foreign language interface (section
9) for storing arbitrary byte-sequences.
Starting with version 3.3, SWI-Prolog offers garbage collection on
the atom-space as well as representing 0-bytes in atoms. Although
strings and atoms still have different features, new code should
consider using atoms to avoid too many representations for text as well
as for compatibility to other Prolog implementations. Below are some of
the differences:
- creation
Creating strings is fast, as the data is simply copied to the global
stack. Atoms are unique and therefore more expensive in terms of memory
and time to create. On the other hand, if the same text has to be
represented multiple times, atoms are more efficient.
- destruction
Backtracking destroys strings at no cost. They are cheap to handle by
the garbage collector, but it should be noted that extensive use of
strings will cause many garbage collections. Atom garbage collection is
generally faster.
String objects by default have no lexical representation and thus can
only be created using the predicates below or through the foreign
language interface (See chapter 9. There are
two ways to make
read/1 read
text into strings, both controlled through Prolog flags. One is by
setting the double_quotes
flag to string
and
the other is by setting the backquoted_string
flag to true
.
In latter case, `Hello world`
is read into a string and
write_term/2
prints strings between backquotes if quoted
is
true
. This flag provides compatibility to LPA Prolog string
handling.
- string_to_atom(?String,
?Atom)
-
Logical conversion between a string and an atom. At least one of the two
arguments must be instantiated. Atom can also be an integer
or floating point number.
- string_to_list(?String,
?List)
-
Logical conversion between a string and a list of character codes
characters. At least one of the two arguments must be instantiated.
- string_length(+String,
-Length)
-
Unify Length with the number of characters in String.
This predicate is functionally equivalent to atom_length/2
and also accepts atoms, integers and floats as its first argument.
- string_concat(?String1,
?String2, ?String3)
-
Similar to atom_concat/3,
but the unbound argument will be unified with a string object rather
than an atom. Also, if both String1 and
String2 are unbound and String3 is bound to text,
it breaks
String3, unifying the start with String1 and the
end with
String2 as append does with lists. Note that this is not
particularly fast on long strings as for each redo the system has to
create two entirely new strings, while the list equivalent only creates
a single new list-cell and moves some pointers around.
- sub_string(+String,
?Start, ?Length, ?After, ?Sub)
-
Sub is a substring of String starting at Start,
with length Length and String has After
characters left after the match. See also sub_atom/5.