The function PosEx returns the position of the first occurrence of SubStr in string S, starting at the character position specified by Offset.

The first character in S is at position 1, the second character in S is at position 2 e.t.c.

SubStr can have any number of characters.

PosEx will return zero (0) -

  • If SubStr is not found in S.

  • If either SubStr or S is an empty string with a length of zero (0).

  • If SubStr is longer than S.

PosEx is case-sensitive so the characters are matched exactly.

Declaration: Function PosEx( const SubStr, S : string; Offset : Integer) : Integer;

Several examples follow showing a comparison to the similar function Pos. Please review this function at Pos.

procedure OnMapEvent(var Value:Variant);
begin
//Starting the search at position 2
LogInfo(IntToStr(PosEx('cd', 'abcdef', 2))); //Result is 3
LogInfo(IntToStr(PosEx('Alice', 'Alice,Bob,Ken,Barbie,Alice,Warren', 2))); //Result is 22
 
//As a comparison. Pos starts the search at position 1 so Alice at position 22 is not found.
LogInfo(IntToStr(Pos('Alice', 'Alice,Bob,Ken,Barbie,Alice,Warren'))); //Result is 1
end;