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 use Search and replace 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
24-Jan-03
Category
Algorithm
Language
Delphi All Versions
Views
95
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Lou Adler

Search and replace

Answer:

Almost every text editor / word processor has the ability to search for a given 
string and replace it with another. If you're planning on adding similar 
functionality to your application, here's an example of where all of it can start:

1   function SearchAndReplace(
2     sSrc, sLookFor, sReplaceWith
3     : string)
4     : string;
5   var
6     nPos, nLenLookFor: integer;
7   begin
8     nPos := Pos(sLookFor, sSrc);
9     nLenLookFor := Length(sLookFor);
10    while (nPos > 0) do
11    begin
12      Delete(sSrc, nPos, nLenLookFor);
13      Insert(sReplaceWith, sSrc, nPos);
14      nPos := Pos(sLookFor, sSrc);
15    end;
16    Result := sSrc;
17  end;


For example, let's say you have a string -- 'this,is,a,test' -- and you want to 
replace the commas with spaces. Here's how you'd call SearchAndReplace():
18  
19  SearchAndReplace('this,is,a,test', ',', ' ')
20  
21  SearchAndReplace() will now return the string 'this is a test'.


			
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