Link to home
Start Free TrialLog in
Avatar of urif
urif

asked on

replacing text within a text file

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
Avatar of Cynna
Cynna

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.
Avatar of urif

ASKER

thx, please if you can post an example.
Avatar of urif

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Cynna
Cynna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of urif

ASKER

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
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.

Avatar of urif

ASKER

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.
No problem, glad I could help....