Reading txt file with multiple columns to insert in Tlistview

Asked

Viewed 3,095 times

4

I have the following function:

var
   Colunas : TStringlist;
   Item: TListItem;
begin
   Colunas := TStringlist.Create;
   Colunas.Text := StringReplace('00:46:30@21/08/2014@Carlos dos Santos@São Paulo',
      '@',Char(13)+Char(10),[rfReplaceAll]);
   Item := LV.Items.Add;
   Item.SubItems.Add(Colunas[0]);
   Item.SubItems.Add(Colunas[1]);
   Item.SubItems.Add(Colunas[2]);

It separates by columns using the @delimiter, in this case I put manually. But, I have a file called client.txt and within it there are several lines of the type:

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

How can I read this file, using this same function above and put everything inside a LISTVIEW ?

Related: Explodes in Delphi

2 answers

6


I was based on a reply from Sozão to propose a way to read a file directly in a TStringlist, because the list can already be accessed "exploded" (using indexes).

var
  Linhas: TStringList;    

...

  Linhas := TStringList.Create;
  try
    Linhas.LoadFromFile(FileName);
    //Agora basta acessar as linhas com Linhas[0], Linhas[1], ..., Linhas[Linhas.Count-1]

  finally
    Linhas.Free;
  end;


Applying the solution to your . txt:

var
  Linhas:  TStringList;
  Colunas: TStringList;
  i:       integer;    

...

  Linhas := TStringList.Create;
  Colunas := TStringlist.Create;
  Colunas.Delimiter := '@';
  Colunas.StrictDelimiter := True;

  try
    Linhas.LoadFromFile( 'cliente.txt' );

    for i := 0 to Linhas.Count-1 do 
    begin
       Colunas.DelimitedText := Linhas[i];
       Item := LV.Items.Add;
       Item.SubItems.Add(Colunas[0]);
       Item.SubItems.Add(Colunas[1]);
       Item.SubItems.Add(Colunas[2]);
    end;

  finally
    Linhas.Free;
  end;

As well pointed out by @Edgar Muniz Berlinck, the code has been improved by using Tstringlist’s own feature to "explode" the string (and improved with the help of @Caputo, which recalled the Strictdelimiter of the D2006+):

We exchanged:

       Colunas.Text := StringReplace(Linhas[i],'@',Char(13)+Char(10),[rfReplaceAll]);

For:

       Colunas.Delimiter := '@';
       Colunas.StrictDelimiter := True;
       ...
       Colunas.DelimitedText := Linhas[i];
  • Just to get rich, Tstringlist owns the properties Delimite and the delimitedText. This is very useful for creating a Tstringlist from a string with delimiters.

  • Yes, you must set the delimite and then the delimitedText in place of text with replace.

  • @Bacco, there’s only one problem in this second code, when the client has spaces, he doesn’t work, like Carlos dos Santos, he only captures Carlos.

  • @Bacco, with the Delimitedtext version, the "ancient" mode works perfectly...

2

.You can use the following code to do this.

That way it gets more elegant is efficient.

procedure TForm1.Button1Click(Sender: TObject);
var
  Coluna, Linha: string;
  Colunas: TArray<string>;
begin
  Linha:= '00:46:30@21/08/2014@João da Silva@São Paulo';

  Colunas:= Linha.Split( [ '@' ] );

  for Coluna in Colunas do
    ShowMessage( Coluna );
end;

The Split is a Helper of the data type string. http://docwiki.embarcadero.com/Libraries/XE3/en/System.SysUtils.TStringHelper.Split

There are other methods Helper in the data type string. Typo Ctrl + Espaço after the variable to see the other methods.

  • Good output the Split too! It would be nice to apply the Ten.SubItems.Add( ... etc in your example, to value the answer.

Browser other questions tagged

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