Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to search and replace strings in a TMemo Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Oct-02
Category
Reporting /Printing
Language
Delphi 2.x
Views
100
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

How to search and replace strings in a TMemo

Answer:

Doing search and replace on strings has been made trivial because of these 3 
functions: Pos(), Delete(), and Insert(). Pos() takes two parameters, a pattern 
search string, and a string to find the pattern in - it returns the location of the 
string, or 0 if it does not exist. Delete() takes three parameters, the string to 
delete from, location of where to start deleting, and how much to delete. 
Similarly, Insert() takes three parameters too. The string that will be inserted, 
the string to insert into, the location to insert.

Many class properties use strings to store values, so one can use this method on 
any of them. For instance, the searching and replacing of an entire TMemo component 
might look like this:

1   procedure TForm1.Button2Click(Sender: TObject);
2   var
3     i: integer;
4     s1: string;
5     SearchStr: string;
6     NewStr: string;
7     place: integer;
8   begin
9     SearchStr := 'line';
10    NewStr := 'OneEye';
11    for i := 0 to Memo1.Lines.Count - 1 do
12    begin
13      s1 := Memo1.Lines[i];
14      repeat
15        Place := pos(SearchStr, s1);
16        if place > 0 then
17        begin
18          Delete(s1, Place, Length(SearchStr));
19          Insert(NewStr, s1, Place);
20          Memo1.Lines[i] := s1;
21        end;
22      until
23        place = 0;
24    end;
25  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC