var s: String; f: TextFile;
AssignFile(f, 'D:\\INPUT.TXT);
Reset(f);
whilenot EOF(f) dobegin
ReadLn(s, f);
ShowMessage(GetField(s, 1)); {The first field\}
ShowMessage(GetField(s, 6)); {The sixth field\}
ShowMessage(GetField(s, 25)); {will return '' if no 25 column...\}end;
CloseFile(f);
{ ==== This function will return a field from a delimited string. ==== \}function GetField(InpString: String; fieldpos: Integer): String;
var
c: Char;
curpos, i: Integer;
begin
curpos := 1;
for i := 1 to fieldpos dobegin
result := ''; if curpos > Length(InpString) then Break;
repeat
c := InpString[curpos]; Inc(curpos, 1);
if (c = '"') or (c = #13) or (c = #10) then c := ' ';
if c <> ',' then result := result + c;
until (c = ',') or (curpos > Length(InpString))
end;
if (curpos > Length(InpString)) and (i < fieldpos) then result := '';
result := Trim(result);
end;
{ ==== This function will trim a string removing spaces etc. ==== \}function Trim(inp_str: String): String;
var
i: Integer;
beginfor i := 1 to Length(inp_str) doif inp_str[i] <> ' ' then Break;
if i > 1 then Delete(inp_str, 1, i - 1);
for i := Length(inp_str) downto 1 doif inp_str[i] <> ' ' then Break;
if i < Length(inp_str) then Delete(inp_str, i + 1, Length(inp_str));
result := inp_str;
if result = ' ' then result := '';
end;
Автор предлагает способ импорта большого CSV-файла в Delphi с помощью функции GetField, которая возвращает поле из строки, а также функции Trim для удаления пробелов и других символов.
Комментарии и вопросы
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.