Search code examples
arraysdelphidynamic-arrays

Putting a line of text into an array delimited by quotes?? Delphi


Okay I am still very new to delphi coding and coding in general. I have researched on splitting strings into an array or list delimited by : or , but in this case I need to do it by a string that is delimited by " ".

Example: "fname","lastname","someplace,state","some business,llc","companyid"

and what I need is the array to be (item[0] = fname) (item[1] = lastname) (item [2] = someplace,state) (item[3] = some business, llc.) (item[4] = companyid)

So as you can see I cannot read in a line of text using the comma as a delimeter because it would throw everything off.

Is there any way to read in a line of text and split it into an array like the example above??


Solution

  • See documentation for TStrings.CommaText.

    Here is an example:

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      System.SysUtils,Classes;
    var sl: TStringList;
      s: String;
    begin
      sl := TStringList.Create;
      try        
        sl.CommaText := '"fname","lastname","someplace,state","some business,llc","companyid"';
        for s in sl do
          WriteLn(s);
        ReadLn;
      finally
        sl.Free;
      end;
    end.
    

    The documentation also says:

    Note: CommaText is the same as the DelimitedText property with a delimiter of ',' and a quote character of '"'.

    So if using DelimitedText, just make sure QuoteChar is " and the Delimiter is ,.