[ Strings and Atoms | Reference Manual | Alphabetic Index ]

string_char(?Index, +String, ?Char)

Succeeds if Char is the value of the Index'th character in String
Variable or integer between 1 and the length of String
Variable or single-character atom


This predicate maps the index position Index to the corresponding character in the given string String. Characters in the string are numbered from 1 (analogous to array indices in subscript/3 and arg/3). Index positions of zero or greater than the string length lead to failure.

The predicate may be used to extract the Index'th character, to find the position(s) of a particular character, or to enumerate all positions and characters in the string.

Modes and Determinism


(4) instantiation fault
String is uninstantated
(5) type error
String is instantiated, but not a string
(24) number expected
Index is instantiated, but not an integer
(5) type error
Char is instantiated, but not an atom
(6) out of range
Index is a negative integer
(6) out of range
Char is a non-characte atom


   string_char(1, "abc", a).          % succeeds
   string_char(3, "abc", C).          % gives C = c
   string_char(I, "abc", c).          % gives I = 3
   string_char(I, "abcb", b).         % gives I = 2 ; I = 4 on backtracking
   string_char(I, "ab", C).           % gives I=1,C=a ; I=2,C=b on backtracking

   string_char(2, "abc", d).          % fails
   string_char(I, "abc", d).          % fails
   string_char(0, "abc", C).          % fails
   string_char(4, "abc", C).          % fails
   string_char(I, "", C).             % fails

   string_char(1, S, c).              % Error 4
   string_char(1, abc, C).            % Error 5
   string_char(1.5, "abc", C).        % Error 5
   string_char(I, "abc", 0'b).        % Error 5
   string_char(-1, "abc", C).         % Error 6
   string_char(I, "abc", bb).         % Error 6

See Also

string_code / 3, string_chars / 2, string_list / 2, char_code / 2