Jump to content

Recommended Posts

Posted

Put an Editbox and a button on the form, leave them named Edit1 and Button 1 respectively.

In Button1.OnClick put the following:

var
msg:string;

begin
msg:=Edit1.text;
StringReplace(msg, 'hai', 'hello', [rfReplaceAll]);
showmessage(msg);
end;

Type

"hai is the same as hai"

into the editbox and click the button.

Why does the resulting message read

"hai is the same as hai"

when it should read

"hello is the same as hello"

as the StringReplace should have replaced all "hai" with "hello"?


Posted (edited)

Strictly speaking, StringReplace is a Function and should be called via a variable ...

procedure TForm1.Button1Click(Sender: TObject);

var msg:string;

begin

msg:=Edit1.text;

msg := StringReplace(msg, 'hai', 'hello', [rfReplaceAll]);

showmessage(msg);

end;

... the above example works perfectly. :w00t:

Edited by FAT64
Posted (edited)

Better still, combine some of the lines ...

procedure TForm1.Button1Click(Sender: TObject);

var msg:string;

begin

msg := StringReplace(Edit1.Text, 'hai', 'hello', [rfReplaceAll]);

showmessage(msg);

end;

:thumbup

Edited by FAT64

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...