Registriert seit: 27. Mai 2005
Ort: Baden
315 Beiträge
Delphi 2007 Enterprise
|
Re: pos von hinten
25. Dez 2007, 14:02
Delphi-Quellcode:
function MyPosEx(const SubStr, s: string; Index: Integer = 1; Upwards: Boolean = False): Integer;
var
i, intSubStrLen: Integer;
begin
Result := 0;
if (Index < 1) then Exit;
if Upwards then
begin
intSubStrLen := Length(SubStr);
if (Index >= Length(s)) then Exit;
if (intSubStrLen < 1) then Exit;
for i := Index to Length(s) do
begin
if (Copy(s, I, intSubStrLen) = SubStr) then
begin
Result := I;
Exit;
end;
end;
end else
begin
intSubStrLen := Length(SubStr);
if (intSubStrLen < 1) then Exit;
for i := Length(s) +1 downto Index do
begin
if (I - intSubStrLen < 1) then Exit;
if (Copy(s, I - intSubStrLen, intSubStrLen) = SubStr) then
begin
Result := I - intSubStrLen;
Exit;
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
const
s = 'abcdefghijklmnopqrstuvwxyz';
c = 'bc';
begin
ShowMessage(Format('MyPosEx of c: %d' + #13#10 + 'MyPosEx of c (down): %d' + #13#10 + 'Pos of c: %d', [
MyPosEx(c, s, 1, True), MyPosEx(c, s), Pos(c, s)]));
end;
Quickedit: sorry es müsste "for i := Length(s) +1 downto Index do" heißen und nicht "for i := Length(s) downto Index do". Jetzt funktioniert es wieder.
|
|
Zitat
|