Solved

StringReplace ... didn't

Posted on 2004-08-04
7
1,183 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
Comment Utility
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
Comment Utility
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-
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Expert Comment

by:-Karamja-
Comment Utility
Doh, Too slow again :(
0
 

Author Comment

by:MartinC
Comment Utility
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-
Comment Utility
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
Comment Utility
Karamji:

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

MartinC
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now