Mega Code Archive

 
Categories / Delphi / Strings
 

Using case...of on Strings

Title: Using case...of on Strings? Question: How can you use case..of on a string? Answer: How can you use case..of on a string? Normally you cannot because it only allows ordinal types (numeric - char is numeric Ord() ). I recently figured (though it isn't a new idea) to create some sort of numeric representation of the string. My favoured way is CRC-32, though you could use any method AS LONG AS the turned number is unique. I won't include the CRC-32 details here, just the ideology/theory. Example: procedure CompareStrings(S: String); begin case Crc32OfString(S) of Crc32OfString('Hello'): // Do wotever... Crc32OfString('Goodbye'): // Do wotever... end; end; See it's as simple as that! It isn't very efficent calling it like that, to optimize it you can HARD CODE the case..of values to speed up the process. I orignally said mail me for the CRC-32 routine but because I've had a few people e-mail me showing interest you can download by whole CRC unit from: http://www.workshell.co.uk/dev/delphi/crc.pas As well as CRC-32, it offers similar routines for CRC-16, Adler32, Kermit16 and other hash routines. I hope its useful to you... Enjoy!