whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

23 Visitors Online


 
...search recursive with the Pos function?
Autor: Dirk Bögelspacher
Homepage: http://www.boegi.de
[ Print tip ]  

Tip Rating (5):  
     


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

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners