Home Contents Index Summary Previous Next

3.20 Representing Text in Strings

SWI-Prolog supports the data type string. Strings are a time and space efficient mechanism to handle text in Prolog. Atoms are under some circumstances not suitable because garbage collection on them is next to impossible (Although it is possible: BIM_prolog does it). Representing text as a list of ASCII values is, from the logical point of view, the cleanest solution. It however has two drawbacks: 1) they cannot be distinguished from a list of (small) integers; and 2) they consume (in SWI-Prolog) 12 bytes for each character stored.

Within strings each character only requires 1 byte storage. Strings live on the global stack and their storage is thus reclaimed on backtracking. Garbage collection can easily deal with strings.

The ISO standard proposes " ... " is transformed into a string object by read/1 and derivatives. This poses problems as in the old convention " ... " is transformed into a list of ASCII characters. For this reason the style check option `string' is available (see style_check/1).

The set of predicates associated with strings is incomplete and tentative. Names and definitions might change in the future to confirm to the emerging standard.

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 ASCII 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 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.

substring(+String, +Start, +Length, -Sub)
Create a substring of String that starts at character Start (1 base) and has Length characters. Unify this substring with Sub. (16)