Solved

StringReplace ... didn't

Posted on 2004-08-04
7
1,188 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is it possible to connect Database from Mobile APK to Website MySQL 3 65
Thread safe  opinion 7 126
code issue 8 97
control image tags in a string ? 12 110
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

10 Experts available now in Live!

Get 1:1 Help Now