Replace words when importing in Listview

Asked

Viewed 85 times

1

I’m importing a file .DAT in a Listview, I need that before the import is done the program looks for the expression {keyword} in the items and that the same expression is replaced by the text of a Edit.

Example:

Original item in file . dat: "7 {keyword wonders}"

Edit text: "Slimming"

Imported item in Listview: "The 7 Wonders of Slimming"

And in the same way with the other items.

How could I do that?

Code:

  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  procedure ListView1Data(Sender: TObject; Item: TListItem);
  procedure BtnLoadFromFileClick(Sender: TObject);
  private
    procedure LoadFromFile(AFileName: string);
  public
    { Public declarations }
  end;

  TMyRecord = record
    name: string;
    address: string;
    floatfield: Single;
    integerfield: Integer;
  end;

var
  Form1: TForm1;
  MyList: TList<TMyRecord>;

Continuation of the Code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  MyList := TList<TMyRecord>.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  MyList.Free;
end;

procedure TForm1.ListView1Data(Sender: TObject; Item: TListItem);
begin
  Item.Caption := MyList[Item.Index].name;
  Item.SubItems.Add(MyList[Item.Index].address);
  Item.SubItems.Add(FloatToStr(MyList[Item.Index].floatfield));
  Item.SubItems.Add(IntToStr(MyList[Item.Index].integerfield));
end;

procedure TForm1.LoadFromFile(AFileName: string);
var
  MyFileStream: TFileStream;
  MyBinaryReader: TBinaryReader;
  temprecord: TMyRecord;
  I, TempNumber: Integer;
begin
  MyFileStream := TFileStream.Create(AFileName, fmOpenRead);
  MyBinaryReader := TBinaryReader.Create(MyFileStream,
    TEncoding.Unicode, false);
  MyList.Clear;
  try
    TempNumber := MyBinaryReader.ReadInteger;
    for I := 0 to TempNumber - 1 do
    begin
      temprecord.name := MyBinaryReader.ReadString;
      temprecord.address := MyBinaryReader.ReadString;
      temprecord.floatfield := MyBinaryReader.ReadSingle;
      temprecord.integerfield := MyBinaryReader.ReadInteger;
      MyList.Add(temprecord);
    end;
    ListView1.Items.Count := MyList.Count;
    MyBinaryReader.Close;
  finally
    MyBinaryReader.Free;
    MyFileStream.Free;
  end;
end;


procedure TForm1.BtnLoadFromFileClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
    LoadFromFile(OpenDialog1.FileName);
end;

end.

1 answer

0


You can use the following code, in which I used two memos and a botão to simulate what you need, just adapt to your code. I tried to detail the code as much as possible but some doubt communicate.

Code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  //Escreo no memo1 algumas frases.
  Memo1.Lines.Add('As 7 maravilhas do {keyword}');
  Memo1.Lines.Add('As 7 maravilhas do nada.');
  Memo1.Lines.Add('As 7 maravilhas do {exemplo} e arredores.');
end;

procedure TForm1.Button1Click(Sender: TObject);
var i, APos: Integer;
    ALine, AText, AWord, AResult: String;
begin
  //O for ai ler ler todas as linhas do memo uma a uma
  for i := 0 to Memo1.Lines.Count - 1 do
    Begin
      //Limpo variáveis
      AText := '';
      AWord := '';
      AResult := '';
      //Vai ler uma minha linha do memo
      ALine := Memo1.Lines[i];

      //Enquanto a linha tiver alguma coisa faz...
      While (Trim(ALine) <> '') do
        Begin
          //Pega a posição de uma nova palavra 
          APos := Pos(' ', ALine);
          //Se encontrou alguma palavra faz...
          if (APos > 0) then
            begin
              //Pega a palavra
              AWord := Trim(AnsiMidStr(ALine, 1, APos - 1));
              //Apaga-a da linha
              Delete(ALine, 1, APos);
            end
          else
            Begin
              //Se não tem mais de uma palavra então pega o resto da linha
              AWord := ALine;
              ALine := '';
            End;

          //verifica se a palavra precisa ser substituída
          if (Trim(AWord) = '{keyword}') then
            Begin
              //Substitui '{keyword}' por 'pais'  
              AResult := AText + ' pais ' + ALine;
              Break;
            End
          else if (Trim(AWord) = '{exemplo}') then
            Begin
              //Substitui '{exemplo}' por 'mundo' 
              AResult := AText + ' mundo ' + ALine;
              Break;
            End
          else
            Begin
              //Se não precisa de substituir junta a palavra ha nova linha 
              AText := AText +' '+ AWord;
            End;
        End;

      //Escreve no memo2 o resultado
      if (Trim(AResult) <> '') then
        Begin
          //Se encontrou uma palavra para substituir
          Memo2.Lines.Add(AResult);
        End
      else
        Begin
          //Se encontrou nada para substituir
          Memo2.Lines.Add(AText);
        End;
    End;
end;
  • Very good. Your code just gave the idea to use the String Replace function that worked the same way.

Browser other questions tagged

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