A helpful Unicode support class in Delphi 2009 is the TCharacter class, which is a sealed class which only consists of static class functions to check whether a character is a Digit, a Letter, etc.
The TCharacter class is the “solution” to eliminate compiler warnings when you combine Chars with Char sets (sets can only contain AnsiChar values, so an expression is changed which results in a compiler warning).
var
C: Char;
begin
// assign some Char value to C
if C in ['a'..'z','A'..'Z'] then
And the set expression should be replaced by a call to IsLetter from the TCharacter class, as follows:
if TCharacter.IsLetter(C) then
While this works for this particular case, sometimes we need a test that doesn’t already exists in the TCharacter class, like the following:
var
C: Char;
begin
// assign some Char value to C
if C in ['a','e','i','o','u'] then
There is no IsVowel function in TCharacter, but the compiler warning itself actually already suggests the CharInSet method from SysUtils, so in this case we can change the code as follows:
var
C: Char;
begin
// assign some Char value to C
if CharInSet(C, ['a','e','i','o','u']) then
Having said that... Although the TCharacter class is a sealed class, we can still extend it with an IsVowel method by using class helpers, as follows:
type
TMyChar = class helper for TCharacter
class function IsVowel(C: Char): Boolean;
end;
The implementation uses the newly mentioned CharInSet function as follows:
class function TMyChar.IsVowel(C: Char): Boolean;
begin
Result := CharInSet(C, ['a', 'e', 'o', 'i', 'u'])
end;
And now we can replace the CharInSet call with a simple call to TCharacter.IsVowel as follows:
var
C: Char;
begin
// assign some Char value to C
if TCharacter.IsVowel(C) then
With no more compiler warnings. Note that the unit that defines the IsVowel method must be added to the uses clause of any other unit where you want to use this functionality.
This tip is the fourth in a series of Unicode tips taken from my Delphi 2009 Development Essentials book published earlier this week on Lulu.com.