有没有更聪明的方法(用更少的代码)从 TDictionary 中获取所有键作为单个字符串,以逗号分隔
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;
Run Code Online (Sandbox Code Playgroud)
只要键本身不包含逗号,您就可以编写:
All_keys_as_string := string.Join(',', FDicList.Keys.ToArray);
Run Code Online (Sandbox Code Playgroud)