Za brojanje reci, ukljuciti RxStrUtils (ne znam za vas, ali je meni Rx obavezan. sad je ukljucen u Jedi [Link mogu videti samo ulogovani korisnici])
Ispod je funkcija koja menja reci, koja garantovano radi, testirana.
Moze se iskoristiti deo code-a za sta vam treba.
type
Delimiters : TCharSet = ['.', ',', '(', ')', '[', ']', #32, #13, #10, #9, ';', ':', '+', '*', '/', '-', '<', '>', '='];
function WordReplace(var Text: AnsiString; ASearchWord, AReplaceWord: AnsiString;
ACaseSensitive: Boolean = True): boolean;
var
i, j : Longint;
ch : char;
PassedBegin, PassedEnd : boolean;
begin
result := false;
PassedBegin := false;
PassedEnd := false;
if ((ASearchWord = '') or (AReplaceWord = '')) then
exit;
if ACaseSensitive then
begin
if AnsiCompareStr(ASearchWord, AReplaceWord) = 0 then
exit;
end
else if AnsiSameText(ASearchWord, AReplaceWord) then
exit;
if pos(#32, ASearchWord) > 0 then //if search word consists of 2 or more words
begin
if not ACaseSensitive then
j := pos(AnsiUpperCase(ASearchWord), AnsiUpperCase(Text))
else
j := pos(ASearchWord, Text);
if j = 1 then
PassedBegin := true
else if j > 1 then
begin
ch := Text[j - 1];
if (ch in Delimiters) then
PassedBegin := true;
end;
if (j + Length(ASearchWord) - 1) = Length(Text) then
PassedEnd := true
else if (j + Length(ASearchWord) - 1) < Length(Text) then
begin
ch := Text[j + Length(ASearchWord)];
if (ch in delimiters) then
PassedEnd := true;
end;
if (PassedBegin and PassedEnd) then
begin
Delete(text, j, Length(ASearchWord));
Insert(AReplaceWord, Text, j);
result := true;
end;
end
else
begin //regular one-word replacement
repeat
if ACaseSensitive then
i := PosWord(ASearchWord, Text, Delimiters)
else
i := PosWord(AnsiLowerCase(Text), AnsiLowerCase(ASearchWord), Delimiters);
if i > 0 then
begin
Delete(Text, i, Length(ASearchWord));
Insert(AReplaceWord, Text, i);
Result := true;
end;
until i <= 0;
end;
end;
|