Search code examples
htmldelphidelphi-7

How do I remove text from before a certain line in delphi


<h1 class="tt">example</h1></div><div class="bl_la_main"><div class="divtext">

I am trying to remove everything before <div class="bl_la_main"> but keep everything after it.

Any help would be appreciated. Thanks


Solution

  • P.S: Since I misunderstood the question, I first created the "take the before terms" function.

    You can take it like this.

    procedure TForm1.Button1Click(Sender: TObject);
    var
     HTML: string;
    begin
     HTML := '<h1 class="tt">example</h1></div><div class="bl_la_main"><div class="divtext">';
     Delete(HTML, Pos('<div class="bl_la_main">', HTML) - 1, Length(HTML) - Pos('<div class="bl_la_main">', HTML));
     ShowMessage(HTML);
    end;
    

    If we need to make it functional;

    function parseHTML(html: string; substr: string): string;
    begin
     Delete(HTML, Pos(substr, HTML) - 1, Length(HTML) - Pos(substr, HTML));
     Result := HTML;
    end;
    

    Use:

    function parseHTML(html: string; substr: string): string;
    begin
     Delete(HTML, Pos(substr, HTML) - 1, Length(HTML) - Pos(substr, HTML));
     Result := HTML;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
     HTML: string;
    begin
     HTML := '<h1 class="tt">example</h1></div><div class="bl_la_main"><div class="divtext">';
     ShowMessage(parseHTML(HTML, '<div class="bl_la_main">'));
    end;
    

    Result:

    <h1 class="tt">example</h1></div">
    

    I created it separately, in a functional way to take both before and after.

    function parseHTMLAfter(html: string; substr: string): string;
    begin
     Delete(HTML, Pos(substr, HTML) - 1, Length(HTML) - Pos(substr, HTML));
     Result := HTML;
    end;
    
    function parseHTMLBefore(html: string; substr: string): string;
    begin
     Delete(HTML, 1, Pos(substr ,html) - 1);
     Result := HTML;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
     HTML: string;
    begin
     HTML := '<h1 class="tt">example</h1></div><div class="bl_la_main"><div class="divtext">';
     ShowMessage(parseHTMLBefore(HTML, '<div class="bl_la_main">'));
    end;
    

    Result:

     <div class="bl_la_main"><div class="divtext">