[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
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:
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:
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.
May 23, 2017: In order to assemble particular subject topics from several files, the program was modified today to append selected records to an existing text file as an alternative to overwriting an existing file with the same name. Version 2.2 posted today reflects the change.
Copyright © 2000-2018, Gary Darby All rights reserved.