Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Replace string inside a HTML and TXT file with content of Memo

Posted on 2014-10-17
9
Medium Priority
?
286 Views
Last Modified: 2014-10-28
Hi,

I need to replace a string inside a HTML and a TXT file with the content of TMemo.
Here the formatting (soft and hard linebreaks) inside the TMemo must be preserved.
Inside the HTML or TXT file the replacement-string is formatted like %myreplacementcode%.

I've tried using StringReplace but all content inside the memo is than placed on a single line.

Any suggestions?
0
Comment
Question by:Delphiwizard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 40386528
Can you show some code, just a few lines how you tried with StringReplace() and it all ended up on a single line.
0
 
LVL 35

Assisted Solution

by:ste5an
ste5an earned 2000 total points
ID: 40386552
The problem with HTML should be obvious: A line break (lf, crlf) in TMemo is not interpreted in HTML as line break. You need to parse those. E.g. by using <br> or a <p>.
0
 

Author Comment

by:Delphiwizard
ID: 40386823
I started simple:
StringReplace(vHTMLString,'%MyMemoContent%', Memo1.Lines.Text, [rfReplaceAll, rfIgnoreCase]);

Open in new window

Then I converted the Memo to String with following function:
function MemoToStr(Sender: TObject) : String;
var i : Integer;
begin
  for i := 0 to (Sender AS TMemo).Lines.Count -1 do
  begin
    if (i <> 0) then
      Result := Result + chr(13);
    Result := Result + Copy((Sender AS TMemo).Lines.Strings[i], 1, Length((Sender AS TMemo).Lines.Strings[i]) -2);
  end;
end;

Open in new window

StringReplace(vHTMLString,'%MyMemoContent%', MemoToStr(Memo1), [rfReplaceAll, rfIgnoreCase]);

Open in new window


So I guess I need to start working on a different kind of parsing.
Any suggestions are more then welkom.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Delphiwizard
ID: 40393722
This is the way to do it.
function MemoToStrHTML(Sender: TObject) : String;
var i : Integer;
begin
  for i := 0 to (Sender AS TMemo).Lines.Count -1 do
  begin
    if (i <> 0) then
      Result := Result + '<br>';
    Result := Result + (Sender AS TMemo).Lines.Strings[i];
  end;
end;

Open in new window

0
 

Author Comment

by:Delphiwizard
ID: 40394336
I've requested that this question be closed as follows:

Accepted answer: 0 points for Delphiwizard's comment #a40393722

for the following reason:

Provided my own answer.
0
 
LVL 35

Accepted Solution

by:
ste5an earned 2000 total points
ID: 40393839
Nope, a helper should be the first choice:

type
  StringsHelper = class helper for TStrings
  public
    function ToHTML: string;
  end;

implementation

function StringsHelper.ToHTML: string;
var
  I: Integer;
begin
  for i := 0 to Self.Count - 1 do
  begin
    if (i <> 0) then
      Result := Result + '<br>';
    Result := Result + Self.Strings[i];
  end;
end;

Open in new window


Then it's a nice Memo.Lines.ToHTML.
0
 

Author Comment

by:Delphiwizard
ID: 40394340
ste5an:
Can you show me how to implement this in real life?
Is it sufficient to add your code to the main unit of the application?
0
 
LVL 35

Expert Comment

by:ste5an
ID: 40394368
Here is a single file sample. Just fill in your code:

unit Unit8;

interface

uses
  System.Classes,
  Vcl.Controls, Vcl.Dialogs, Vcl.Forms, Vcl.StdCtrls;

type
  TForm8 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

  MemoHelper = class helper for TMemo
  public
    function ToHTML: string;
  end;

  StringsHelper = class helper for TStrings
  public
    function ToHTML: string;
  end;

var
  Form8: TForm8;

implementation

{$R *.dfm}

procedure TForm8.Button1Click(Sender: TObject);
begin
  ShowMessage(Memo1.ToHTML);
  ShowMessage(Memo1.Lines.ToHTML);
end;

{ MemoHelper }

function MemoHelper.ToHTML: string;
begin
  Result := 'HTML from TMemo';
end;

{ StringsHelper }

function StringsHelper.ToHTML: string;
begin
  Result := 'HTML from TStrings';
end;

end.

Open in new window


The helper classes should be placed in separate files.

Either you a fan of you own big helper file, then you only need one to place in the uses. Or one file per helper base class. So I would place them into Vcl.StdCtrls.Helper.pas and System.Classes.Helper.pas. Or one file per helper class Helper.Memo.pas and Helper.Strings.pas.

The number and naming depends on how you want to organize your namespaces.
0
 

Author Comment

by:Delphiwizard
ID: 40408136
Thank you ste5an
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question