Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate string's with pipe

I have a line with the following description: '|0200|4|SALGADOS|||KG|00|19051000||||17|'

I want to separate where the pipe to save data in the database.

I'm using the pos function incorrectly. But I am getting the data.

Inside the if then i will insert data into db.

ReadLn(txt, line);
if True then
  if (Pos('|0200|', line)) = 1 then
  begin
    fArq.Add(line);
  end;
  if (pos('|0000|', line)) = 1 then
  begin
    fArq.Add(line);
  end;
  if (pos('|0005|', line)) = 1 then
  begin
    fArq.Add(line);
  end;
  if (pos('|C460|', line)) = 1 then
  begin
    fArq.Add(line);
    flagCF := True;
  end
  else
  begin
    if flagCF = True then
      if (pos('|C490|', line)) = 0 then
        fArq.Add(line)
      else
        flagCF := False;
  end
like image 381
Pablo Aquino Avatar asked Feb 11 '26 21:02

Pablo Aquino


1 Answers

You can also use TStringList:

lStringList := TStringList.Create;
lStringList.delimiter := '|';
lStringList.DelimitedText := '|0200|4|SALGADOS|||KG|00|19051000||||17|';

Now you can access each field using lStringList.Items[ index ]


Note (from comments): if space characters is included in the string, set StrictDelimiter to true to avoid treating them as delimiters.

like image 79
Hans Avatar answered Feb 13 '26 14:02

Hans