Search code examples
delphidelphi-xe7

Delphi XE7 TStrings.DelimitedText


I want to populate a TStringList and get a comma-delimited quoted result.

According to what I have read, it is possible, but I just get a comma-delimited result, no quotes. I cannot get it to drop duplicates.

procedure TForm5.BitBtn1Click(Sender: TObject);
var
  sl : TStringList;
  s : string;
begin
  sl := TStringList.Create;
  try
    sl.Delimiter := ',';
    sl.QuoteChar := '"';
    sl.Duplicates := dupIgnore;
    //sl.DelimitedText := '"1","2","3"';
    sl.DelimitedText := '1,2,3';
    sl.Add('a');
    sl.Add('2');
    s := sl.DelimitedText;
    ShowMessage(s);
  finally
    sl.Free;
  end;
end;

I keep getting var s set as 1,2,3,a,2, but I am expecting "1","2","3","a" instead.


Solution

  • try this:

    procedure TForm1.FormCreate(Sender: TObject);
    var
      sl : TStringList;
      s : string;
    begin
      sl := TStringList.Create;
      try
        sl.Delimiter := ',';
        sl.QuoteChar := #0;  // default = '"'
        sl.Duplicates := dupIgnore;
        sl.DelimitedText := '"1","2","3"';
        sl.Add('a');
        sl.Add('"2"');
        s := sl.DelimitedText;
        ShowMessage(s);
      finally
        sl.Free;
      end;
    end;