Reading Incorrect txt File

Asked

Viewed 135 times

2

Well I’m reading the following information from a txt

0016009993|GuilhermeLima|Azul|4|21|basico+completo|6

after reading I am separating the contents using the delimiters "|" to separate into memos, with the code below.

  begin
    Linha.Delimiter:='|';
    Linha.DelimitedText:=Texto[i];
    cod:=Linha.Strings[0];
    nome:=Linha.Strings[1];
    cor:=Linha.Strings[2];
    codv := linha.strings[3];
    idadade:=Linha.Strings[4];
    tipo:=Linha.Strings[5];
    id:=Linha.Strings[6];
    try
      memo1.lines.add(cod);
      memo2.lines.add(nome);
      memo3.lines.add(cor);
      memo7.Lines.Add(codv);
      memo4.Lines.Add(idade);
      memo5.Lines.Add(tipo);
      memo6.Lines.Add(id);
    except
      ShowMessage('Erro!!! A base de dados não está no padrão correto!');

So far so good, but if the txt is that way

0016009993|Guilherme Lima|Azul|4|21|basico+completo|6

(a space in the name, which would be correct)

values go wrong for memos, why does this occur?

2 answers

2


If you were using a current version of Delphi you could use Linha.StrictDelimiter, however in is not possible.

Alternatively, you can do it this way:

linha.DelimitedText := '"' + StringReplace(Texto[i], linha.Delimiter, '"' + linha.Delimiter + '"', [rfReplaceAll]) + '"';

They made this question similar to Soen, I believe the answer fits your case.

  • Strictdelimiter line := True; solved my problem.

  • What version of your Android?

1

I had a similar problem, but I use another way and I recommend you use the split procedure

procedure Split(const Delimiter: Char;Input: string; const Strings: TStrings);
begin
Assert(Assigned(Strings)) ;
Strings.Clear;
Strings.Delimiter       := Delimiter; 
strings.StrictDelimiter := True;
Strings.DelimitedText   := Input;
end; 

leaving "Strictdelimiter" as true it ignores whitespace and only separates the variable with the given character.

call as follows:

Split([caracter de referencia],[variavel de origem], [stringlist que irá receber os valores]);

ie will call the split as follows:

Split('|', '0016009993|GuilhermeLima|Azul|4|21|basico+completo|6', Linha);

As a hint, as you are passing the values for different memos, do not save in variables, do a FOR or a WHILE that read the stringlist until the end and already save the value in the memo, will give a speedy in your work. And if you need to use this value later take it straight from the memo.

hope I’ve helped!

Browser other questions tagged

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