Рекурсивный поиск с помощью функции Pos
Материал из DRKB
{
Function PosN get recursive - the N th position of "Substring" in
"Mainstring". Does the Mainstring not contain Substrign the result
is 0. Works with chars and strings.
}
function PosN(Substring, Mainstring: string; n: Integer): Integer;
begin
if Pos(Substring, Mainstring) = 0 then
begin
Result := 0;
Exit;
end
else
begin
if n = 1 then
Result := Pos(Substring, Mainstring)
else
begin
Result := Pos(Substring, Mainstring) + PosN(Substring, Copy(Mainstring,
(Pos(Substring, Mainstring) + 1), Length(Mainstring)), n - 1);
end;
end;
end;
// Beispiele / Examples
i := PosN('s', 'swissdelphicenter.ch', 2);
// i=4
i := PosN('x', 'swissdelphicenter.ch', 1);
// i=0
i := PosN('delphi', 'swissdelphicenter.ch', 1);
// i=6
Source: http://www.swissdelphicenter.ch
ID: 00881