Title: A better Pos function
Question: 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.
Answer:
This function searchs string s1 in s2 and returns the position it, like the old pos does. In addition, you can define a start position and a maximum length for to search.
function StrPos (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;
begin
Result:= -1;
l1:= Length (s1);
l2:= Length (s2);
if max if pos if (l1 max) then Exit; // maybe we're ready at all?
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+1; Exit; end;
end;
end;
end;