Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

replacing text within a text file

Posted on 2002-07-02
8
Medium Priority
?
189 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Accepted Solution

by:
Cynna earned 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

810 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