There are two predicates for determining the length of a text object:
These two predicates are identical except that
will report an error if its first argument is not a text object.
There are versions of Quintus Prolog on stock hardware that support Kanji. Those versions currently represent Kanji by pairs of characters. Beware of this difference. This is likely to change.
There are two predicates for extracting a character from a text object:
arg/3; see the description of
library(args), lib-tma-arg. Text must be instantiated to a text object. Index, if instantiated, must be an integer. If Index is less than one or greater than the length of Text,
string_char/3fails quietly. If Index is a variable,
string_char/3will enumerate suitable values for Index and Char.
string_char/3except that Offset counts from 0 rather than from 1. This predicate was added in this release to simplify conversion from another dialect, which is why it is inconsistent with Prolog conventions. We recommend that you use
string_char/3in new programs instead.
| ?- string_size(fred, X). X = 4 | ?- string_size(47, X). no | ?- string_length(fred, X). X = 4
| ?- string_length(47, X). ! Type error in argument 1 of string_length/2 ! symbol expected, but 47 found ! goal: string_length(47,_43) | ?- X is " ". X = 32 | ?- string_char(3, 'an example', X). X = 32
| ?- nth_char(2, 'an example', X). X = 32 | ?- string_char(I, 'an example', 0'a). I = 1 ; I = 6 ; no | ?- nth_char(I, 'an example', 0'a). I = 0 ; I = 5 ; no
We shall see in the next section that
have been defined by
nth_char(Offset, Text, Char) :- subchars(Text, [Char], Offset, 1, _).
If you wanted a predicate like
nth_char/3 but that counted from
the right-hand end of the text instead of the left-hand end, you
nth_char_from_right(Offset, Text, Char) :- /* this is not in library(strings) */ subchars(Text, [Char], _, 1, Offset).