Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

StringReplace ... didn't

Posted on 2004-08-04
7
Medium Priority
?
1,219 Views
Last Modified: 2012-08-13
I am working away on Delphi 7 on an XP-equipped comp and using something I THOUGHT was very simple: the StringReplace function. I have StrUtils in my Uses clause, and the compiler happily accepted the code but it did NOT do the replace. I tried AnsiReplaceText as well, and it failed similarly. So then I wrote the following simple test program :

procedure TfrmRotator.Button1Click(Sender: TObject);
var
  s:string;
begin
  s := Edit1.text;     // originally contained 'Edit1'
  ReverseString(s);                                                  //A
  StringReplace(s,'di','xo',[rfReplaceAll]);                   //B
  AnsiReplaceText(s,'t','e');                                       //C
  Delete(s,3,1);                                                       //D
  Edit1.text := s;
end;

Of lines A,B,C and D, only D worked. Anyone know why?

Martin C

0
Comment
Question by:MartinC
  • 3
  • 2
  • 2
7 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11716784
Does "reversestring" change the "Edit1" to "1tidE" ?

Is so, then B would have nothing to find.

procedure TfrmRotator.Button1Click(Sender: TObject);
var
  s:string;
begin
  s := Edit1.text;     // originally contained 'Edit1'
  ReverseString(s);                                                  //A   <-- reserves text?
  StringReplace(s,'di','xo',[rfReplaceAll]);                   //B   <-- won't find "di", try "id" ??
  AnsiReplaceText(s,'t','e');                                       //C
  Delete(s,3,1);                                                       //D
  Edit1.text := s;
end;

0
 
LVL 7

Accepted Solution

by:
LRHGuy earned 240 total points
ID: 11716818
Try saving the results!

procedure TfrmRotator.Button1Click(Sender: TObject);
var
  s:string;
begin
  s := Edit1.text;     // originally contained 'Edit1'
  S:=ReverseString(s);                                                  //A
  S:=StringReplace(s,'di','xo',[rfReplaceAll]);                   //B
  S:=AnsiReplaceText(s,'t','e');                                       //C
  Delete(s,3,1);                                                       //D
  Edit1.text := s;
end;


Larry
0
 
LVL 2

Expert Comment

by:-Karamja-
ID: 11716827
procedure TfrmRotator.Button1Click(Sender: TObject);
var
  s:string;
begin
  s := Edit1.text;     // originally contained 'Edit1'
  s := ReverseString(s);                                                  //A   <-- reserves text?
  s := StringReplace(s,'di','xo',[rfReplaceAll]);                   //B   <-- won't find "di", try "id" ??
  s := AnsiReplaceText(s,'t','e');                                       //C
  s := Delete(s,3,1);                                                       //D
  Edit1.text := s;
end;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:-Karamja-
ID: 11716835
Doh, Too slow again :(
0
 

Author Comment

by:MartinC
ID: 11717514
Sheesh, that never occurred to me ... I just can't pick these. Ever noticed how Delphi Help has examples of all the things you couldn't possible get wrong but leaves out examples of ones like this ...     ;-)

Points to LRHGuy. Bad luck to Karamja.

Martin C
0
 
LVL 2

Expert Comment

by:-Karamja-
ID: 11718211
Np,
Thats why there is this site, http://www.delphibasics.co.uk take a look when you have some spare time
0
 

Author Comment

by:MartinC
ID: 11721835
Karamji:

That's an excellent site! Thanks for the tip.

MartinC
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question