Text Search

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]



Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only



Problem Description

Here is a simple program to search a text file  for a user specified string. 

Background & Techniques

The above description applies to Version 1 of the program.  Version 2.0 adds some user requested features:

  • Ability to output to display only, to a user specified file name, or both.
  • Ability to  output selected records, non-selected records, or both.
  • For matched records, ability to keep any combination  of
    • Text before the matched string
    • The matched string
    • The text following the matched string.

Notes for Programmers

Version 1 Notes

The substring search function, Pos, is used to perform the basic search of each line of the input text file.  

If the "Case sensitive" checkbox is checked, the search is performed "as is".  If "Case Sensitive" is not checked, then both the input line and the input string are converted to upper case before the "Pos" search is performed.

If "Whole words only" is checked a call is made to function IsWord   which returns true two conditions are met:  

  • The position before the first letter of the search string is a delimiter or 0 (i.e. the string begins at the beginning of the line). 
  • The position after the last letter of the search string is a delimiter or past the end of the line (i.e. the string ends at the end of the line). 

One more check box, "Show hits only" ,  allows user  to display only the lines with matches or  to display the entire input file with matched strings  highlighted.   In order to highlight the matches strings, a TRichEdit is used.  The matched strings are selected using Selstart and SeLength properties and setting SelAttributes  to make the font style bold.  The same technique could also apply color or other style properties to matches.   One trick is used  to highlight multiple occurrences of the search string in the displayed input line - each occurrence is replaced by  non-displayable characters in the working copy of the line before the next search is performed.

Version 2 Notes 

The "Show hits only" checkbox has been replaced by a number  of checkboxes to allow maximum flexibility for selecting and outputting. Nothing difficult about the coding except that there are now 9 checkboxes which means 2^9 (512) configurations to check for correct handling!   I confess to not having checked them all, but I have checked that the common ones  work as stated.  I'll count on users to report any errors that I missed.  Using mnemonic names for the boxes is a good practice to simplify testing in complex cases.    

February 20, 2014:     Version 2.1 posted today, now "remembers" from run to run the options chosen for text searching.   

Download source

Download executable


Created 17 Jan 17 2004  

Modified 18 Feb 2016



  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2016, Gary Darby    All rights reserved.