Jump to content

Delphi: Why isn't StringReplace working!


Recommended Posts

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"?

Link to comment
Share on other sites


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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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