Explodes in Delphi

Asked

Viewed 745 times

2

I have a txt file in which data is saved as follows:

00:46:30@21/08/2014@Carlos dos Santos@são Paulo

In PHP I would use the explode to turn each die before "@" into columns.

How do I do this in Delphi ? I need each separate die @ turn a column, so I can put them in a LISTVIEW.

1 answer

5


A possible technical repair1 is to use a Tstringlist:

var
   Colunas : TStringlist;

begin
   Colunas := TStringlist.Create;
   Colunas.Text := StringReplace('00:46:30@21/08/2014@Carlos dos Santos@São Paulo',
      '@',Char(13)+Char(10),[rfReplaceAll]);

Tstringlist internally uses the sequence CR LF to separate items, so the above code transforms a long line into several separate items, by changing the @ in sequence CR LF

Edit: As well remembered by Edgar Muniz Berlinck, the Tstringlist has a property to break the string by other delimiters, thereby:

var
   Colunas : TStringlist;

begin
   Colunas := TStringlist.Create;
   Colunas.Delimiter := '@';
   Colunas.DelimitedText := Linhas[i];

1. gambiarra

  • thanks for the answer. I didn’t understand one thing, how will I use it ? type I wanted to use as follows $coluna1, $coluna2, for each text before the @.

  • thanks. Remembering that I have a file called client.txt and within it several lines of this. If you can help me with that too I appreciate it, it should go through this whole file.

  • @user7605 to read the file line by line I think it’s best to ask a separate question.

  • I can test here now ?

  • handed out manually. Now I need to do by reading a txt file, I must ask another question ?

  • @user7605 I think it’s better, just not to mix the two subjects. So this question is for those who in the future need the explode for other things.

  • I’m doing @Bacco, thank you so much!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.