Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Find and Replace Type Thing Posted by MoKoMoKo on 9 Dec 2001 at 7:21 AM
'ello all...

Wonder if anyone can help...You know how most word processors and hex editors have a Find and Replace function (where the program searches thru' the file for a given string, and replaces it with a different string) would I be able to implement such a thing in Pascal? For example, get Pascal to open any file and change all occurrences of 'Apple' to 'Orange' or whatever...

Thanx...
Laterz all...
MoKo
Report
Re: Find and Replace Type Thing Posted by Manning on 9 Dec 2001 at 10:35 AM
: 'ello all...
:
: Wonder if anyone can help...You know how most word processors and hex editors have a Find and Replace function (where the program searches thru' the file for a given string, and replaces it with a different string) would I be able to implement such a thing in Pascal? For example, get Pascal to open any file and change all occurrences of 'Apple' to 'Orange' or whatever...

Sure thats possible. I would open 2 files. 1 for input, 1 for output. Read a line from the infile, process it with Replace (see below for function), and write it to the outfile. When you reach the end of the infile, close and delete it. Then rename the outfile to the original name of the infile, and you're done.

{ S = The line of text to find/replace in }
{ Old = The text to delete }
{ New = The text to add }
{ Example: Replace('I want an apple', 'apple', orange' }
{          That would return 'I want an orange' }
{ Note..this is done from memory and untested...should work tho }
function Replace(S, Old, New: String): String;
begin
     while (Pos(Old, S) > 0) do
     begin
          Insert(New, S, Pos(Old, S));
          Delete(S, Pos(Old, S), Length(Old));
     end;
     Replace := S;
end;




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.