1

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??

1 Answers1

4

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 ,.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • 2
    beat me by seconds... Worth noting that this won't work going the other way. `WriteLn(sl.DelimitedText);` will produce `fname,lastname,"someplace,state","some business,llc",companyid`, quoting only those fields that require it. – J... Sep 24 '14 at 15:49
  • @LU I was looking to add an additional specification to this, I would like to skip the first line of the text file I am reading from. I thought I would use the Seek() however I could not get that to work. Any suggestions or documentation you could point me to that would answer this? – Delphi Noob Oct 01 '14 at 13:58
  • If you read the complete file into a TStringList (yourList.LoadFromFile('aFilename'), just skip processing the first item in the list. If the file is too large to process in memory, follow this example: [How Can I Efficiently Read The First Few Lines of Many Files in Delphi](http://stackoverflow.com/a/4857887/576719), that shows how to dynamically read one line at a time. – LU RD Oct 01 '14 at 15:22