Solved

replacing text within a text file

Posted on 2002-07-02
8
181 Views
Last Modified: 2013-11-18
hi everyone,

is there a way to open a text file, search for a text, and replace all that with an empty string?

for example i have the text in a file:

hello
hello world what's up
bye

i want to search for "hello world" and replace it with "blank/empty string" or delere that text from the file so at the end i'll have

hello
what's up
bye

thanks
0
Comment
Question by:urif
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7124551
This is very easy - there are lots of ways.
One of the easiest is using TStringList to grab file in
memory, and then using StringReplace procedure to replace
your text.
If you wish, I'll post you example that does this.
0
 

Author Comment

by:urif
ID: 7124574
thx, please if you can post an example.
0
 

Author Comment

by:urif
ID: 7124576
oh, i forgot to mention, i'll be using this in a console application, so a lot of the vcl components maybe are not available.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
Cynna earned 50 total points
ID: 7124611
You'll need TStringList, which uses Classes, and StringReplace (defined in Sysutils). I presume this won't be a problem for console app.

Copy/Paste:
----------------

function FileTextReplace(FileName, OriginalString, ReplacementString: String): Boolean;
var i: Integer;
    s: String;
begin
 Result:=FALSE;
 with TStringList.Create do begin
     try
         if not FileExists(FileName) then Exit;
         // Grab file into TStringList:
         LoadFromFile(FileName);
         // Copy file contents to single string variable:
         s:=Text;
         // Replace string (look at help for StringReplace() function, for further info):
         s:=StringReplace(s, OriginalString, ReplacementString, [rfIgnoreCase]);
         // Copy modified file back to TStringList:
         Text:=s;
         // And finally save it to same file:
         SaveToFile(FileName);
         Result:=TRUE;
     finally
        Free;
     end;
 end;
end;


DEMO:
-------
(place Button1 on Form1)

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Delete 'hello world' from C:\bla.txt:
  if FileTextReplace('C:\bla.txt', 'hello world', '') then ShowMessage('Replaced OK.')
                                                      else  ShowMessage('Error!');
end;
0
 

Author Comment

by:urif
ID: 7124720
i refrace the question and let me know if i should put it as a different one, so you can get the points:


the function works fine, i modified it a bit to suit my program. the problem is that one of the strings i need to replace is random, meaning, it could be anything, it's not a fixed string. what i know is that it is enclosed like in HTML and XML inside <> and that the open tag is <name> and the close is </name>

so basically i need find the opening tag and delete the string beginning (and including) the open tag all the way up to the close tag. example:

line 1 some text
line 2<mytag>some random string</mytag>
line 3 some other text

i need to delete the whole thing at the end i'll have

line 1 some text
line 2
line 3 some other text
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7124854
urif,

Oh well...this is a bit more complicated then what you
initially stated.
I can write you a function that will accept input MyTag,
and delete everything in between (including tags), like
you specified.

To be clear, take few examples:

a) file1.txt looks like this:

line 1 some text
line 2<title>some random string</title>
line 3 some other text

b) file2.txt looks like this:

line 1 some text
line 2<title>some random string</title>
line 3<bla>text</bla> text2 <blabla> text3 <blabla>


Lets take our function declaration for example:
 CutTags(FileName, TagName: String)

Then, a call like this:

a)  CutTags('file1.txt', 'title') would change file1.txt to this:
line 1 some text
line 2
line 3 some other text

b)  CutTags('file2.txt', 'bla') would change file1.txt to this:

line 1 some text
line 2<title>some random string</title>
line 3 text2 <blabla> text3 <blabla>


If you can wait a day or two, I'll write this for 50 points.
If you want it ASAP, you'll get it in about 1 hour for 100 points.

0
 

Author Comment

by:urif
ID: 7124875
it's ok thx so much!

i'll figure it out.

actually i placed another question before placing this one here and i think it was answered.

again, thanks.
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7124900
No problem, glad I could help....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 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