Mega Code Archive

 
Categories / Delphi / Examples
 

Extractdelimited strings

Extract strings from long (delimited) text... 1) Here's a real easy way - just wrote for a current project: procedure CsvImport; var strWork : String; importFile : TextFile; i : Integer; dbFields : TStringList; begin { open import file } AssignFile(importFile,fneInputFile.FileName); FileMode := 0; { read-only } Reset(importFile); dbFields := TStringList.Create; try while NOT Eof(importFile) do begin Readln(importFile,strWork); dbFields.Clear; dbFields.CommaText := strWork; for i := 0 to dbFields.Count - 1 do dbFields[i] := DelChars(dbFields[i],Chr(34)); { process the fields now as desired - each are in the TStringList } end; finally CloseFile(importfile); dbFields.Free end; end; Using the above with your data would result in: dbFields[0] = C dbFields[1] = 20-09-06 dbFields[2] = MOU etc.... 2) You can use CommaText property on TStrings object (and his inheritors): ... var L: TStringList; S: String; ... L.CommaText := S; // Now L[0] is the first field, L[1] - second and so on... 3) BTW, there is undocumented function in Delphi 5 "classes" unit - ExtractStrings: function ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings): Integer;