Solved

find and replace in strings

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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