?
Solved

Searching through a Textfile

Posted on 2003-03-19
6
Medium Priority
?
181 Views
Last Modified: 2010-04-04
I have a textfile.
Each block of the Textfile begins with a title which is enclosed in square brackets.
i.e.
[TITLE]
Data within Block.

At the moment my textfile is being read into and displayed in a RichEdit box.
What I want to do is search through my textfile for each Title and then display all titles in a memo(without the square brackets).
Is there a simple pre-defined search routine which will do this? Or where do I start if I have to write my own search routine?

Thanks in advance guys!
0
Comment
Question by:Degsy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 

Accepted Solution

by:
da-vinci earned 100 total points
ID: 8169772
Yes there is a simple search routine.
I always use the search routines from madshi. (http://www.madshi.net/)

There are a lot of possibilities with these string routines. It took me 3 minutes to make your request:



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StrUtils, madStrings, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
    i,
    til: Integer;
    title: String;
begin
    Memo1.Clear;
    for i:= 0 to RichEdit1.Lines.Count-1 do
    begin
        if TextMatch(RichEdit1.Lines[i], '[*]') then     // TRUE if title exists
        begin
            til:=  PosStr(']', RichEdit1.Lines[i]);      // Position of ']'
            title:= MidStr(RichEdit1.Lines[i], 2, til-2);// capture title
            Memo1.Lines.Add(title);
        end;
    end
end;

end.

0
 
LVL 1

Expert Comment

by:jturpin
ID: 8169779

Try reading it into a TStringList usinf the LoadFromFile method.

Then locate the '[' using IndexOf etc. Then copy the title out and put it in your RichEdit.

Of course if each line begins with '[' simply find ']' and copy the text into your RichEdit.

for i:= 0 to StringList1.Count-1 do
begin
  dx:= Pos(']', StringList1.Strings[i];
  if (dx > 0) then
    RichEdit1.Lines.Add(Copy(StringList1.Strings[i],2,dx))
  else
    RichEdit1.Lines.Add('>> Untitled: Copy(StringList1.Strings[i],2,10));
end; {for i}

John.
0
 

Expert Comment

by:da-vinci
ID: 8169801
So forgot to tell:
1) download and install madshi package
2) put "madStrings" in your uses

That should be enough to have:
PosStr
TextMatch
StrMatch
UpChar
etc.
0
 

Expert Comment

by:CleanupPing
ID: 9316677
Degsy:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 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