Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

StringReplace

I am trying to replace substrings within a string using the function StringReplace.

But I get an error:

"[Error] SChtmlemail.dpr(151): Incompatible types"

I have following in my code:

type
    TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);

var
  TemplateFields : array of record
    Name : string;
    Value : string;
  end
  s : string;
  flags : TReplaceFlags;

begin
 flags := [rfReplaceAll];
 s := StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, flags);
end;


I have tried following variations of StringReplace:

StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, flags);
StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, [rfReplaceAll]);
StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, 'rfReplaceAll');

but none works. What am I doing wrong?

0
tommykjensen
Asked:
tommykjensen
  • 3
  • 3
  • 2
1 Solution
 
DaFoxCommented:
Hi.

These calls should work:

StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, flags);
StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, [rfReplaceAll]);

This one doesn't (it's not of type TReplaceFlags, it's a String!):

StringReplace(s,TemplateFields[n].Name,TemplateFields[n].Value, 'rfReplaceAll');

There is no error in your code. s, TemplateFields[n].Name, TemplateFields[n].Value are all string type!

Something wrong with your dynamic array?
Where does the error occur?

Markus
0
 
marcoszorrillaCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
s:String;
r:String;
begin
Oldp:='h';
newp:='b';
s:='holaholaholahola';
r:=StringReplace(s,'hola','bola',[rfReplaceAll]);
ShowMessage(r);
end;


Best Regards
Marcos
0
 
marcoszorrillaCommented:
Pardon Oldp and Newp were string variables that I tested before the code above.




procedure TForm1.Button1Click(Sender: TObject);
var
s:String;
r:String;
begin
s:='holaholaholahola';
r:=StringReplace(s,'hola','bola',[rfReplaceAll]);
ShowMessage(r);
end;

Best Regars.
Marcos.


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tommykjensenAuthor Commented:
The error occurs during compile.
0
 
DaFoxCommented:
Yep, but where is line 151 in your snippet?
0
 
tommykjensenAuthor Commented:
Line 151 is the line with Stringreplace
0
 
DaFoxCommented:
Okay, the problem is that you're redefining TReplaceFlags. Delete this line
-> TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
and you're done.

Markus
0
 
tommykjensenAuthor Commented:
Yep that did it. Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now