Solved

StringReplace ... didn't

Posted on 2004-08-04
7
1,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 60 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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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