|
Kanade's Delphi Stuff
Delphi Tips, Sample code and Tools
Copyright ©
1997-2004, Sanjay Kanade
| |
|
|
|
Delphi Tips: String processing
|
Tip 17:
Passing strings to Windows API functions
(modified: 11 Nov 1999)
Tip 22:
Parsing a text block to break into lines
(modified: 18 Nov 1999)
Tip 27:
Equivalent of isspace() from C library
(modified: 23 Nov 1999)
Tip 16:
Coming from a C background
(modified: 16 Feb 2001)
Tip 44:
Parsing a string to get the tokens
(modified: 16 Feb 2001)
|
|
|
|
17: Passing strings to Windows API functions
|
Date added/modified: 11 Nov 1999
This is the problem I faced soon after I started using Delphi. Calls to Win API were not working and the Delphi help was not of much use in pinpointing the problem. The Delphi IDE newsgroup at Borland was very helpful as I got many answers to my problem.
The problem is that 'string' is a dynamic entity whereas all Windows functions which return something in a char buffer assume that you have pre-allocated buffers. Hence, before passing a string to a Windows function, you need to preset its length so that that much buffer space is pre-allocated. Then, you need to pass a PChar typecast to the Windows function as it expects the data to start at the 0th byte. In 'string', 0th byte is used for its length. A PChar typecast points to the proper first data position to which the Windows API function can refer to. Finally, after returning from Windows API function, you need to set the length back to the actual length returned. Here is a sample code:
SetLength(aString, MAX_PATH);
GetTempPath(MAX_PATH, PChar(aString)); //WinAPI call
SetLength(aString, strlen(PChar(aString)));
|
|
|
|
22: Parsing a text block to break into lines
|
Date added/modified: 18 Nov 1999
Suppose a text block contains a number of lines separated by new line characters (cr/lf). You need to create a TStringList such that it gets one line for each line in the text block. What will you do? There is no need to parse. It is easy. Just set the Text property of the TStringList object to the text block and the parsing will be done by TStringList itself.
|
|
|
|
27: Equivalent of isspace() from C library
|
Date added/modified: 23 Nov 1999
I couldn't find an equivalent function of isspace() from C library. Then, I did some testing with trim() which is supposed to trim the white space around a string. Consequently, I made up the following function. If anyone knows better, please inform me.
function isWhiteSpace(c: char): boolean;
begin
result := ord(c) <= 32;
end;
|
|
|
|
16: Coming from a C background
|
Date added/modified: 16 Feb 2001
No doubt, strings are a great help in Object Pascal. When I occasionally go back to writing C or C++ code, I miss this feature. You can pass them around, even return them as results, can put them as class members without initializing and Object Pascal takes care of all the housekeeping.
However, coming from a C background, I was looking for certain function equivalents, and I didn't find them. For example, I couldn't find an equivalent of strtok. I also couldn't find a function to strip spaces and had to implement my own strip. It's only recently that I found a bunch of useful functions in Delphi help. They don't start with str as I had assumed. Previously, when I looked under the keyword 'string handling,' I always opted for the "null" terminated which showed me only the str functions. It's the other link which shows others like Trim, IsDelimiter, and so on which I missed so far. Needless to say, Trim is the stripping function and IsDelimiter helps to tokenize. You may have to create an equivalent of strtok with the help of IsDelimiter.
I made a function skuGetTokens to solve my tokenizing problems.
|
|
|
|
Copyright 1995-2004, Sanjay Kanade. All rights reserved. All trademarks and copyrights belong to their respective owners.
|
|
|
|