Mega Code Archive

 
Categories / Delphi / Examples
 

Replace text in a tedit

procedure ReplaceText(Edit: TCustomEdit; strOLD, strNEW: string); var x, Position: integer; tmpstr, tmpstr2: string; begin tmpstr := Edit.Text; for x := 0 to Length(Edit.Text) do begin if Copy(Edit.Text, x, Length(strold)) = strold then begin tmpstr := Copy(Edit.Text, 0, x - 1) + strnew; Position := x; end; end; tmpstr2 := Edit.Text; if Position <> 0 then Edit.Text := tmpstr + Copy(tmpstr2, Position + Length(strOLD), Length(tmpstr2)) else Edit.Text := tmpstr; end; // Example/Beispiel: procedure TForm1.Button1Click(Sender: TObject); begin ReplaceText(Edit1, 'OldWord', 'NewWord'); end; // With the StringReplace function: procedure ReplaceText(Edit: TCustomEdit; strOLD, strNEW: string); begin Edit.Text := StringReplace(Edit1.Text, strOLD, strNEW, [rfReplaceAll]); end;