Solved

find and replace in strings

Posted on 1998-12-26
3
156 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

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…
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…
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…

820 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