
05.03.2009, 13:40
|
 |
Активный
|
|
Регистрация: 22.09.2007
Адрес: SPb
Сообщения: 228
Версия Delphi: 7, 2009, XE2
Репутация: 70
|
|
Aristarh Dark, да, но я хотя бы попытался ответитить на вопрос Cros'а, а не "отписался" бесполезной (в данном вопросе) функцией.
Главная идея это разбить строку на слова. А мою функцию можно использовать как пример для создания более универсальной функции, которая будет учитывать различные разделители слов.
Хотя бы так:
Код:
function ReplaceStr(S, Srch, Replace: string; IgnoreCase: boolean): string;
var
i, l: integer;
tmp: string;
ReplaceFlags: TReplaceFlags;
begin
Result:= '';
l:= Length(S);
if l < 1 then exit;
tmp:= '';
if IgnoreCase then ReplaceFlags:= [rfIgnoreCase] else ReplaceFlags:= [];
for i:= 1 to l do
begin
if (s[i] in [' ', ',', '.', ':']) or (i = l) then
begin
if i = l then tmp:= tmp + s[i];
if (Trim(tmp) = Trim(Srch)) then
tmp:= StringReplace(tmp, Srch, Replace, ReplaceFlags);
if i < l then tmp:= tmp + s[i];
Result:= Result + tmp;
tmp:= '';
end else tmp:= tmp + s[i];
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
x1 = 'one two one three oneone';
x2 = 'one two one three oneone one';
x3 = 'one,two,one,three,oneone';
x4 = 'one, two, one, three, oneone';
x5 = 'one, two one three oneone one';
var
s: string;
begin
s:= ReplaceStr(x1, 'one', 'zero', false);
s:= s + #13#10 + ReplaceStr(x2, 'one', 'zero', false);
s:= s + #13#10 + ReplaceStr(x3, 'one', 'zero', false);
s:= s + #13#10 + ReplaceStr(x4, 'one', 'zero', false);
s:= s + #13#10 + ReplaceStr(x5, 'one', 'zero', false);
ShowMessage(s);
end;
__________________
Начинающий программист уверен, что в 1 килобайте 1000 байт.
Законченный программист уверен, что в 1 километре 1024 метра.
|