Home > Back-end >  get all keys as single string from TDictionary class
get all keys as single string from TDictionary class

Time:03-06

is there any smarter way ( with less code) to go get all keys from a TDictionary as a single string, comma separated

var
  FDicList : TDictionary <String, Integer>;
  KeyStrList: TStringlist;
  KeyName: string;
begin

  /// result as comma text
  KeyStrList := TStringlist.Create;
  try
    for KeyName in FDicList.Keys do
      KeyStrList.Add(KeyName);

    All_keys_as_string := KeyStrList.CommaText;
  finally
    KeyStrList.Free;
  end; 

end;

CodePudding user response:

As long as the keys don't contain a comma itself you can just write:

All_keys_as_string := string.Join(',', FDicList.Keys.ToArray);
  • Related