|
INPRISE Online And ZD Journals Present:
The VCL class TStrings is a powerful class with many features you may not be aware of. One such feature is the CommaText property. Using the CommaText property, you can convert the text in a TString object from plain text to comma-separated text and back again to plain text. In this article, we'll show you how.
Conversion functions
Figure A: Our example program displays plain text and comma-separated text.
Listing A: Function to convert text to comma-separated text
Listing B: Function to convert back from comma-separated text
Both functions take arguments for two pointers to objects derived from TStrings. These pointers point to a source and destination for the conversion. ConvertToCommSep() also takes an argument--itemsPerRecord--that specifies the number of items to put on each line (record) of comma-separated text. ConvertFromCommaSep() really requires no explanation. It derives all its functionality from the CommaText property of TStrings. ConvertToCommaSep() is only slightly more complicated. ConvertToCommaSep() first checks to see if the number of items to be converted is a multiple of itemsPerRecord. If it isn't, an exception is thrown. Next, ConvertToCommaSep() creates a temporary TStringList object to use while building records with the correct number of items. ConvertToCommaSep() iterates through the source text, adding itemsPerRecord items to the temporary TStringList. Once a record contains the correct number of items, it's added to the destination TStrings object using the CommaText property of the temporary TStringList. This is the point at which the actual conversion takes place. Listing C shows how easily we use these two functions in our example application to perform the conversion in either direction. Listing C: Using the conversion functions
As we comma-separate Back to Top Back to 1999 Index of Articles Copyright © 1999, Ziff-Davis Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of Ziff-Davis Inc. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis is prohibited. |