Moin,
ich habe folgende funktion(eine von vielen) geschrieben:
Delphi-Quellcode:
function AddTok(const S, T: string; const C: Char; CS: Boolean = False): string;
var
SLen, SIdx, ResIdx, TokStart, TokLen: Integer;
TokExists: Boolean;
CurrentTok: string;
begin
if T <> '' then
begin
SLen := Length(S);
SetLength(Result, SLen+Length(T)+1);
ResIdx := 0;
TokExists := False;
TokStart := 0;
TokLen := 0;
for SIdx := 1 to SLen do
begin
if (S[SIdx] <> C) or ((ResIdx > 0) and (Result[ResIdx] <> C)) then
begin
Inc(ResIdx);
Result[ResIdx] := S[SIdx];
end;
if S[SIdx] <> C then
begin
if TokStart = 0 then
TokStart := SIdx;
Inc(TokLen);
end;
if ((S[SIdx] = C) or (SIdx = SLen)) and (TokStart > 0) then
begin
CurrentTok := Copy(S, TokStart, TokLen);
if not TokExists then
TokExists := ((CS) and (lstrcmp(PChar(CurrentTok), PChar(T)) = 0)) or
((not CS) and (lstrcmpi(PChar(CurrentTok), PChar(T)) = 0));
TokStart := 0;
TokLen := 0;
end;
end;
if (ResIdx > 0) and (Result[ResIdx] = C) then
SetLength(Result, ResIdx-1)
else SetLength(Result, ResIdx);
if not TokExists then
if Result <> '' then
Result := Result + C + T
else Result := Result + T;
end else
Result := S;
end;
Sie tut nichts anderes, also ein neues wort(T) ans Ende anzufügen, SOFERN es nicht bereits vorkommt (S). Die Wörter selbst sind durch einen Seperator getrennt (C). CS gibt an ob ein vorhandenes Wort mit dem hinzuzufügenden case sensitive sein muss. Am Ende muss der String (im Erfolgsfall) auch sauber wieder zurückgegeben werden, d.h wenn S am Anfang so aussieht:
Zitat:
....Wort1..Wort2.........Wort3....Wort4..
und ich "Wort5" anfügen will, muss es am Ende so aussehen:
Zitat:
Wort1.Wort2.Wort3.Wort4.Wort5
Die Funktion (s.o) funktioniert.... nur kann man sie noch schneller machen?