program ReplaceFirstOccurrence;
procedure ReplaceFirstOccurrence(var str: string; const oldSub, newSub: string);
var
posIndex: Integer;
beforePart, afterPart: string;
begin
posIndex := Pos(oldSub, str);
if posIndex > 0 then
begin
beforePart := Copy(str, 1, posIndex - 1);
afterPart := Copy(str, posIndex + Length(oldSub), Length(str));
str := beforePart + newSub + afterPart;
end;
end;
var
str: string;
oldSub, newSub: string;
begin
str := 'aa bb cc dd ee cc';
oldSub := 'cc';
newSub := 'YY';
ReplaceFirstOccurrence(str, oldSub, newSub);
WriteLn(str);
end.
(*
run:
aa bb YY dd ee cc
*)