1

I am trying to add following values to

procedure TForm1.FormCreate(Sender: TObject);
var
  md: TDictionary<string, string>;
  s, v: string;
begin
  md := TDictionary<string, string>.Create;
  try
    s := 'orange';
    v := 'fruit';
    md.Add(s, v);

    s := 'orange ';
    v := 'color';
    md.Add(s, v);

    ShowMessage(IntToStr(md.Count));
  finally
    md.Free;
  end;
end;

I know this is duplicate but if you look at second orange, you can see a space at the end. I think Delphi trims the value but it shouldn't.

Does anyone know solution of this problem?

Thanks,

blacksun
  • 733
  • 7
  • 24
  • 5
    I just test your code and the dictionary doesn't trim the keys, Which version of Delphi are you using? – RRUZ Sep 07 '12 at 03:11
  • Please don't post fake code. Please post real code. Code that compiles. Use the clipboard to achieve this. – David Heffernan Sep 07 '12 at 09:33
  • @RRUZ you are right. This one worked fine but i don't know why the real one didn't work. I didn't paste the real one because it reads from a file and it is big code of a record file. Btw, I use XE2 – blacksun Sep 07 '12 at 13:34
  • Is this updated code still the code that works or the 'real' one that didn't work? – GolezTrol Sep 07 '12 at 13:40
  • Ok that works very well indeed. I believe there was a mistake. Works like a charm. I will use this for multilanguage (localizing) – blacksun Sep 07 '12 at 14:58

1 Answers1

4

This code shows a message box containing the number 2 on all known versions of Delphi. That is exactly as is expected and the TDictionary code most certainly does not trim your keys when comparing for equality.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • +1. Tested. I get the expected `2`, and if I remove the space (which trimming it would do) I get an exception about duplicates not being allowed (as expected). – Ken White Sep 07 '12 at 14:35
  • Yes worked great! I think I was doing a mistake. I am going to use this to localize my software. Works very fast indeed..! – blacksun Sep 07 '12 at 15:09
  • Here is why i choosed this method (adding space at the end). I am translating Hour and Hours to Turkish. But bothe of them translated to Saat. To reverse language, i had to add space 'Saat '. This is how i have solved the problem... – blacksun Sep 07 '12 at 15:20
  • @DavidHeffernan thank you. I didn't know about that :) I am doing it right now. – blacksun Sep 07 '12 at 16:15