… or where is the substring? The old fashioned Pos function is the most common way for searching in a string. But it always begins on the start of the string and so its a little bit useless to search behind the first position of the string.

Lets have a new Pos function to solve this! This function searchs string s1 in s2 and returns the position of it, like the old Pos does. In addition, you can define a start position and a maximum length for to search.

function StrIPos (const s1, s2: string;
                  pos, max: Integer): Integer;
// s1 = the string that must be found
// s2 = the string to search
// pos = start position
// max = maximum search length
var s, l1, l2, j: Integer;
 Result:= -1;
 l1:= Length (s1);
 l2:= Length (s2);
 if max <= 0 then max:= l2; // maximum length
 // we can not search before the first char
 if pos < 0 then pos:= 0;
 // maybe we're ready at all?
 if (l1 <= 0) or (pos > max) then Exit;
 for s:= pos to (max - l1) do begin
  for j:= 1 to l2 do begin
   if (s1[j] <> s2[s+j]) then
    BREAK; // Diffrence detectet
   // we have it found
   if (j = l1) then begin Result:= s; Exit; end;