next up previous contents index
Next: Operators Up: Built-in predicates Previous: Classifying characters   Contents   Index


Representing text in strings

SWI-Prolog supports the data type string. Strings are a time and space efficient mechanism to handle text text in Prolog. Strings are stores 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 (foreign) 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 systems. Below are some of the differences:


\begin{itemlist}
\item [creation]
Creating strings is fast, as the data is simp...
...garbage collections. Atom garbage collection is
generally faster.
\end{itemlist}

See also the prolog-flag double_quotes.

string_to_atom2?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_list2?String, ?List Logical conversion between a string and a list of ASCII characters. At least one of the two arguments must be instantiated. string_length2+String, -Length Unify Length with the number of characters in String. This predicate is functionally equivalent to atom_length2 and also accepts atoms, integers and floats as its first argument. string_concat3?String1, ?String2, ?String3 Similar to atom_concat3, 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_string5+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_atom5.


next up previous contents index
Next: Operators Up: Built-in predicates Previous: Classifying characters   Contents   Index
Dr. Richard Botting 2001-12-12