Solved

how do i find text, find next  and replace text?

Posted on 1998-12-10
8
688 Views
Last Modified: 2010-04-04
how do i find text, find next and replace text in a memo field??? and not have the search case sensitive?
0
Comment
Question by:hyper66
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
BoRiS earned 150 total points
ID: 1350357
hyper66

How thing going over there, long time no hear...here is some simple code to search a memo and replace it with other text...

procedure TForm1.Button2Click(Sender: TObject);

var
  i : integer;
  s1 : string;
  SearchStr : string;
  NewStr : string;
  place : integer;
begin
  SearchStr := 'line';
  NewStr := 'OneEye';
  for i := 0 to Memo1.Lines.Count -1 do begin
    s1 := Memo1.Lines[i];
    Repeat
      Place := pos(SearchStr, s1);
      if place > 0 then begin
        Delete(s1, Place, Length(SearchStr));
        Insert(NewStr, s1, Place);
        Memo1.Lines[i] := s1;
      end; //if-then
    until place = 0;
  end; //for-loop
end;

if you need some more help just leave a comment...

Later
BoRiS
0
 

Author Comment

by:hyper66
ID: 1350358
Hi Boris, It's been along time...Cold and cloudy for the most part..what is the url to your new webpage?

Sorry boris i did'nt explain what i was looking for good enough.. What i ment find and replace and find next useing Find dialog and replace dialog..
0
 

Author Comment

by:hyper66
ID: 1350359
Adjusted points to 150
0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1350360
hyper66

Ok, here is some code to use the find/replace dialog to find text and replace it...

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceDialog1.Position := Point(Memo1.Left + Memo1.Width, Memo1.Top);
 ReplaceDialog1.Execute;
end;

procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin
  with Memo1 do
  begin
    { begin the search after the current selection if there is one }
    { otherwise, begin at the start of the text }
    if SelLength <> 0 then
      StartPos := SelStart + SelLength;
    else
      StartPos := 0;
    { ToEnd is the length from StartPos to the end of the text in the rich edit control }
    ToEnd := Length(Text) - StartPos;
    FoundAt := FindText(ReplaceDialog1.FindText, StartPos, ToEnd, [stHideMatchCase]);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(ReplaceDialog1.FindText);
    end;
  end;
end;

and for the replace...

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var
  SelPos: Integer;
begin
  with TReplaceDialog(Sender) do
  begin
    { Perform a global case-sensitive search for FindText in Memo1 }
    SelPos := Pos(FindText, Memo1.Lines.Text);
    if SelPos > 0 then
    begin
      Memo1.SelStart := SelPos - 1;
      Memo1.SelLength := Length(FindText);
      { Replace selected text with ReplaceText }
      Memo1.SelText := ReplaceText;
    end
    else MessageDlg(Concat('Could not find "', FindText, '" in Memo1.'), mtError, [mbOk], 0);
  end;
end;

Later
BoRiS
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350361
Hi hyper66, BoRiS,

BoRiS

FindText(...  is not available in MemoComponent,

Hyper66,

not the best solution follows,

Function StrUp (S : String) : String;
{ used for not casesensitive }
var i : Integer;
begin
  for i := 1 to length(s) do s[i] := UpCase(s[i]);
  result := s;
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
{ Only for ReplaceButton in ReplaceDialog }
var
  SelPos: Integer;
  SelTmp: Integer;
begin
  if Memo1.sellength > 0 then
    Selpos := Memo1.SelStart + Memo1.SelLength
  else
    SelPos := Memo1.SelStart;
  if selpos = 0 then
    Selpos := pos(StrUp(ReplaceDialog1.FindText),StrUp(memo1.text))
  else
  begin
    SelTmp := Selpos;
    Selpos := pos(StrUp(ReplaceDialog1.FindText),copy(StrUp(memo1.text),selpos,length(memo1.text)- selpos )) - 1;
    if selpos > 0 then selpos := selpos + seltmp;
  end;
  if SelPos > 0 then
  begin
    Memo1.SelStart := SelPos - 1;
    Memo1.SelLength := Length(ReplaceDialog1.FindText);
    Memo1.SelText := ReplaceDialog1.ReplaceText;
  end
  else MessageDlg(ReplaceDialog1.FindText + ' not found in Memo1', mtError, [mbOk], 0);
end;

meikl

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350362
hi hyper66,

this works like find and replace in wordpad
wholeword and raplace all are not implemented
you can search casesensitive or not



unit f_r_u;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    ReplaceDialog1: TReplaceDialog;
    procedure Button1Click(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
    procedure ReplaceDialog1Close(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var
  oldpos : integer; {Hold CursorPos before enter Dialog}


procedure TForm1.Button1Click(Sender: TObject);
begin
  oldpos := memo1.selstart;
  Replacedialog1.FindText := copy(Memo1.text,memo1.selstart+1,memo1.sellength); {If selected something then take it over}
  ReplaceDialog1.Execute;
end;

Function StrUp (S : String) : String;
{ Use for none CaseSensitivity }
var i : Integer;
begin
  for i := 1 to length(s) do s[i] := UpCase(s[i]);
  result := s;
end;


procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  FCase : Integer;
  SelTmp: Integer;
  SelPos: Integer;
begin
  { Check For CaseSens }
  if frmatchcase in replacedialog1.Options then
    fCase := 1
  else fcase := 0;
  if Memo1.sellength > 0 then
    Selpos := Memo1.SelStart + Memo1.SelLength
  else
    SelPos := Memo1.SelStart;
  case fCase of
    0 : Begin
          if selpos = 0 then
            Selpos := pos(StrUp(ReplaceDialog1.FindText),StrUp(memo1.text))
          else
          begin
            SelTmp := Selpos;
            Selpos := pos(StrUp(ReplaceDialog1.FindText),copy(StrUp(memo1.text),selpos,length(memo1.text)- selpos )) - 1;
            if selpos > 0 then selpos := selpos + seltmp;
          end;
        end;
    1 : Begin
          if selpos = 0 then
            Selpos := pos(ReplaceDialog1.FindText,memo1.text)
          else
          begin
            SelTmp := Selpos;
            Selpos := pos(ReplaceDialog1.FindText,copy(memo1.text,selpos,length(memo1.text)- selpos )) - 1;
            if selpos > 0 then selpos := selpos + seltmp;
          end;
        end;
  end;
  if SelPos > 0 then
  begin
    Memo1.SelStart := SelPos - 1;
    Memo1.SelLength := Length(ReplaceDialog1.FindText);
  end
  else MessageDlg(ReplaceDialog1.FindText + ' not found in Memo1 nicht finden.', mtError, [mbOk], 0);
  memo1.setfocus; {Force Memo, to show selection}
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
  if Memo1.sellength > 0 then
    if frmatchcase in replacedialog1.Options then
      if Replacedialog1.Findtext = copy(memo1.text,memo1.selstart+1,memo1.sellength) then
        Memo1.SelText := ReplaceDialog1.ReplaceText
      else
        Replacedialog1find(self)
    else
      if StrUp(Replacedialog1.Findtext) = StrUp(copy(memo1.text,memo1.selstart+1,memo1.sellength)) then
        Memo1.SelText := ReplaceDialog1.ReplaceText
      else
        Replacedialog1find(self)
  else
    Replacedialog1find(self)
end;

Procedure TForm1.ReplaceDialog1Close(Sender: TObject);
begin
  memo1.selstart := oldPos;
  memo1.sellength := 0;
end;

end.


Meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350363
hi hyper66,

replace the
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);

with this (replaces and searchnext automatically!)

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
  if Memo1.sellength > 0 then
    if frmatchcase in replacedialog1.Options then
    begin
      if Replacedialog1.Findtext = copy(memo1.text,memo1.selstart+1,memo1.sellength) then
      begin
        Memo1.SelText := ReplaceDialog1.ReplaceText
      end
    end
    else
      if StrUp(Replacedialog1.Findtext) = StrUp(copy(memo1.text,memo1.selstart+1,memo1.sellength)) then
         Memo1.SelText := ReplaceDialog1.ReplaceText;
  Replacedialog1find(self)
end;


meikl
0
 

Author Comment

by:hyper66
ID: 1350364
Thanks guys.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

12 Experts available now in Live!

Get 1:1 Help Now