Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Copy and Text From / To

Ho all,

I have an html page and I need to copy all html code from:

<head>  to </head>  //...this includes the head tags and put it in a string variable.

Hope you can help

thx

st3vo
0
ST3VO
Asked:
ST3VO
  • 2
1 Solution
 
ThievingSixCommented:
function CopyText(const S: String; FromText, ToText: String): String;
var
  PosStart,
  PosEnd,
  PosLen : Integer;
begin
  Result := '';
  PosStart := Pos(S,FromText);
  PosEnd := Pos(S,ToText);
  PosLen := PosEnd - PosStart;
  If PosLen < 1 Then Exit;
  Result := Copy(S,PosStart,PosLen + Length(ToText));
end;

MyString := CopyText(MyHTMLData,'<head>','</head>');
0
 
epasquierCommented:
ThievingSix has a working function in this case but for generic use I recommend the one bellow. Main difference is if ToText is a substring of FromText or if ToText can appear in given string before FromText, CopyText will not work properly. He should have used PosEx to find PosEnd only after the FromText

usage is same :

MyString :=SearchBetweenAndCopy(MyHTMLData,'<head>','</head>');
function SearchBetweenAndCopy(Str:String; const StrBefore,StrAfter :String) :String;
Var
 P,P2:Integer;
begin
 P:=Pos(StrBefore,Str);
 Result='';
 if P>0 Then
  begin
   P:=P+Length(StrBefore);
   P2:=PosEx(StrAfter,Str,P);
   if P2>P Then 
    begin
     P:=P-Length(StrBefore);
     P2:=P2+Length(StrAfter);
     Result:=Copy(Str,P,P2-P);
    end;
  end;
end;

Open in new window

0
 
ST3VOAuthor Commented:
Perfect! Thanks!!!
0
 
ST3VOAuthor Commented:
Hi epasquier,

Yes, you where right, it didn't work ... I had already assigned the points though :o/

Sorry about that!

If you can help, I now need to delete instead of copy ...

I've opened a question here:

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_24992364.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now