Search code examples
delphitdictionary

How do i fill a TDictionay from a list of comma-separated string pairs?


I want to have a text file with a list of strings (say for example comma separated with key and values that I can use for auto replacement) and store each key-value pair in a TDictionary<string, string>. How do I populate the dictionary?


Solution

  • If you only have one-to-one key-value relation (not like three key words "apple" and "apples" and "McIntoshes" would be turned into "McIntosh") - then probably the simpliest way would be to use TStringList, providing that

    1. U make your file exactly of Key=Value lines, not Key,Value
    2. U either need it case sensitive or do UpperCase over the file.

    Then u use http://docwiki.embarcadero.com/Libraries/XE3/en/System.Classes.TStrings.Values

    To speed things up a bit you can use THashedStringList of IniFiles unit. There also was somethign similar in JCL in JclXML unit.