Mega Code Archive

 
Categories / Delphi / Functions
 

Search recursive with the Pos function

Title: search recursive with the Pos function? function PosN(Substring, Mainstring: string; n: Integer): Integer; { Function PosN ermittelt rekursiv - die N te Position von "Substring" in "Mainstring". Ist der String nicht enthalten, Ergebnis 0. Falls N zu hoch ist, wird das letzte gefundene Vorkommen angezeigt. Funktioniert mit Chars und Strings } { 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. } begin if Pos(substring, mainstring) = 0 then begin posn := 0; Exit; end else begin if n = 1 then posn := Pos(substring, mainstring) else begin posn := 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