The is_string/2 operator forces Prolog to evaluate string expressions just like
arithmetic expressions with is/2. The right expression of
is_string/2 is evaluated to a string and that value is unified with the
left operand. If the left operand is a list then the result string is converted to a list
of character codes. For more information see working with strings.
The is_string/2 operator can be used with null atoms, a null atom is an atom of length 0,
to specify a null atom just use two single quotes: ''.
It is possible to use arithmetic expressions as arguments of string predicates that take integer parameters. For example: the last parameter of the string predicate insert/3 is an integer. This parameter determines where a string is inserted into another string and an expression like 1+3 can be used: insert( abcdefghijkl, x, 1+2 ). See arithmetic in Prolog for more information about arithmetic expressions.
see also: delete/3 expand_sq/1insert/3 is/2 substring/3 string_list/2 to_lower/1 to_string/2 to_upper/1 working with strings
| Examples | |
| X is_string "hello " & "world". | X = "hello world" |
| X is_string 'hi' & ' everybody'. | X = 'hi 'everybody' |
| 'hello' is_string 'hi'. | fails |
| 'hi' is_string 'hi'. | yes |
| X is_string insert("a", "bc", 1). | X = "bca" |
| [H|Tail] is_string "hello". | H = 104, Tail = [101, 108, 108, 111] |
| X is_string insert("a", "bc", 3). | fails |
| Pos is 2, X is_string insert(abcdef, x, 1+Pos). | succeeds, the arithmetic expression is evaluated
to 3 and the result is: Pos = 2 X = abcxdef |
| X is_string '' | X = '' |
| Exceptions | |
| argument in expression is an empty variable | an instantiation_error exception is thrown |
| an atom with arguments that is not a built-in predicate | a type_error(evaluable, Predicate_indicator) exception is thrown, Predicate_indicator is a predicate indicator for the incorrect atom |
| operator which is not defined for string or arithmetic expressions | a type_error(evaluable, Predicate_indicator) exception is thrown |
| The value of an integer is too large | an evaluation_error(int_overflow) exception is thrown |