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

49 Visitors Online


 
...use some expanded Pos-functions?
Autor: Heiko Schröder
Homepage: http://www.1pw.de
[ Print tip ]  

Tip Rating (7):  
     


// Get the Position of a string, starting at the end
// Rückwärtiges Vorkommen einer Zeichenkette innerhalb eines strings, Position von hinten

function LastPos(SearchStr, Str: string): Integer;
var 
  
i: Integer;
  TempStr: string;
begin
  
Result := Pos(SearchStr, Str);
  if Result = 0 then Exit;
  if (Length(Str) > 0) and (Length(SearchStr) > 0) then
  begin
    for 
i := Length(Str) + Length(SearchStr) - 1 downto Result do
    begin
      
TempStr := Copy(Str, i, Length(Str));
      if Pos(SearchStr, TempStr) > 0 then
      begin
        
Result := i;
        break;
      end;
    end;
  end;
end;

// Search for the next occurence of a string from a certain Position
// Nächstes Vorkommen einer Zeichenkette ab einer frei definierbaren Stelle im string

function NextPos(SearchStr, Str: string; Position: Integer): Integer;
begin
  
Delete(Str, 1, Position - 1);
  Result := Pos(SearchStr, upperCase(Str));
  if Result = 0 then Exit;
  if (Length(Str) > 0) and (Length(SearchStr) > 0) then
    
Result := Result + Position + 1;
end;

// Get the number of characters from a certain Position to the string to be searched
// Anzahl der Zeichen von einer definierbaren Position zur gesuchten Zeichenkette

function NextPosRel(SearchStr, Str: string; Position: Integer): Integer;
begin
  
Delete(Str, 1, Position - 1);
  Result := Pos(SearchStr, UpperCase(Str)) - 1;
end;

// simple replacement for strings
// einfaches Ersetzen von Zeichenketten

function ReplaceStr(Str, SearchStr, ReplaceStr: string): string;
begin
  while 
Pos(SearchStr, Str) <> 0 do
  begin
    
Insert(ReplaceStr, Str, Pos(SearchStr, Str));
    Delete(Str, Pos(SearchStr, Str), Length(SearchStr));
  end;
  Result := Str;
end;


 

Rate this tip:

poor
very good


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