Solved

find and replace in strings

Posted on 1998-12-26
3
149 Views
Last Modified: 2010-04-04
Hi experts,

I am searching a source code which replaces all occurencies of an "old" substring in a master-string
to a "new" substring. If the "old" substring can't be found in the master-string, the routine must be
quit without run-time error.

Have you suggestions for me how this could be accomplished?

With kind regards

Mathes
0
Comment
Question by:mathes
3 Comments
 
LVL 8

Accepted Solution

by:
ZifNab earned 30 total points
ID: 1353243
Hi mathes,

this should be a way.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ReplaceDialog1: TReplaceDialog;
    Edit1: TEdit;
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceDialog1.Execute;
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var position : integer;
    MasterStr : String;
begin
 MasterStr := edit1.text;
   repeat
     position := Pos(replaceDialog1.findtext, MasterStr);
     if position <> 0 then begin
      Delete(MasterStr,position,Length(replaceDialog1.findtext));
      Insert(ReplaceDialog1.ReplaceText, MasterStr,position);
     end;
   until position = 0;
   Label1.Caption := MasterStr;
end;
end.

Regards, Zif.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1353244
hi mathes,

other method

in a function

function replace_it(SourceS, OldS, NewS : String) : String;
var
  P : Integer;
  STmp, STmp2 : String;
begin
  STmp2 := '';
  STmp := SourceS;
  P := pos(OldS,STmp);
  while p <> 0 do
  begin
    STmp2 := STmp2 + copy(STmp,1,p-1)+NewS;
    delete(STmp,1,p+Length(OldS)-1);
    P := pos(OldS,STmp);
  end;
  STmp2 := STmp2 + STmp;
  result := STmp2;
end;

to call example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.text := replace_it(memo1.text,edit1.text,edit2.text);
end;

meikl
0
 

Author Comment

by:mathes
ID: 1353245
Hi experts,

thank you for your excellent advice.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

14 Experts available now in Live!

Get 1:1 Help Now