Replace words when importing in ListView

1

I am importing a .DAT file into a ListView , so before the import is done the program looks for the expression {keyword} in the items and that same expression is replaced by the text of an Edit .

Example:

  

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

     

Edit Text : "Weight Loss"

     

Item imported into Listview : "The 7 Wonders of Weight Loss"

And in the same way with the other items.

How could I do this?

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>;

Code Continuation:

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.
    
asked by anonymous 14.09.2017 / 14:40

1 answer

0

You can use the following code, where I used two memos and one botão to simulate what you need, just adapt it to your code. I've tried to make the code as detailed as possible, but do not hesitate to let me know.

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;
    
14.09.2017 / 17:20