[top] [up] [next]

Strings


It is possible to work with strings just like with arithmetic expressions. To force Trinc-Prolog to evaluate string expressions the is_string/2 operator must be used. Below there is an overview of the operators and predicates that can be used for string evaluation. All these string predicates are not defined in the ISO Prolog standard.

Name / arity Explanation
&/2 string concatenation operator, the result is the two strings added together
delete/3 delete part of a string
expand_sq/1 replace each single quote character with two single quote characters
insert/3 insert a string into another string
is_string/2 force Trinc-Prolog to perform string evaluation
replace/3 replace all occurences of a string by another string
substring/3 copy a substring from a string
string_list/2 converts between a string and a list of integers
to_prolog/1 convert an expression to a valid Prolog string. A valid Prolog string is a string which can be used as input for a Prolog compiler.
to_string/2 convert an expression to a string (not a valid Prolog string)
to_upper/1 convert an expresison to uppercase
to_lower/1 convert an expresison to lowercase
trim/1 remove trailing and leading whitespace characters from a string
triml/1 remove trailing whitespace characters from a string
trimr/1 remove leading whitespace characters from a string

The left operand of the is_string/2 operator is unified with the result of the string evaluation. If the left operand is a list then the variables in the list are unified with the ASCII values of the characters in the result string.

Examples  
X is_string "hello " & "world". X = "hello world"
[H|Tail] is_string "hello". H=104, Tail=[101, 108, 108, 111]
X="hi", X is_string "hi". X="hi"
B= 12.4, X is_string "B=" & to_string(B, true). X = "B=12.4"In case the left operand is a list the result string is converted to a list of character codes.  

With predicates like insert/3, delete/3 and substring/3 a position of a character inside a string must be specified. The first character in a string has index 1. It is possible to use arithmetic expressions in arguments of a predicate where an index value is expected.  

Examples  
X is_string insert("puter", "com", 1). X="computer"
X is_string substring("prolog", 2, 1+2). X="rol"
Y=1, X is_string delete("computers", Y*3, Y+1). Y=1, X="couters"

[top] [up] [next]

 

info@trinc-prolog.com