index(+string_or_atom, +string_or_atom, ?integer)

This arithmetic predicate locates a substring within a string. The string which is searched is the first argument, the string to search for is the second argument and the third argument is the position from where the search is started. The character positions start at 1. The result is the position of the substring within the string, if the substring was not found then the length+1 of the string is returned. If the third argument is of the incorrect type a type_error(evaluable, A) exception is thrown.

see also: arithmetic in Prolog is/2

Examples
X is index("searching for a substring", " a", 1). X = 14
X is index("searching for a substring", " a", 20). X = 26 (string was not found)
X is index(search, a, 1). X = 3

 

Exceptions
The string argument Arg is not a valid atom or string argument a type_error(evaluable, Arg) exception is thrown

 

 

info@trinc-prolog.com