Solved

StringReplace ... didn't

Posted on 2004-08-04
7
1,197 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 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 93
add combobox item based on numbers 9 154
Strange behavior when a form is closed 6 60
firemonkey Android Listview Sort items 7 46
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

789 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