Delete Characters that do not begin with "http" in TMemo

Hello All;

  I need to Delete all Characters in a TMemo that do not begin
With the               http://

Lets say that you have these sets:


http://somedomain.com
http://www.somedomain.com
some/thing/like/this.html
some/other/thing/


2nd:
I need to get rid of any link that begins with

http://wrongdomain.com/
http://www.wrongdomain.com/

At the end of the above URL's is added text links
To sub-categories. I need to have ANYTHING that begins
With the beginning Main-URL to be removed from the TMemo.
( They all very, and are never the same )

Thank You
carrzkiss
LVL 31
Wayne BarronAuthor, Web DeveloperAsked:
Who is Participating?
 
EvarestConnect With a Mentor Commented:
If you have on each line either text or a hyperlink, you can use the following:

for Q :=Memo1.Lines.Count -1 downto 0 do
 begin
  if pos( 'http://', lowercase(Memo1.Lines[Q]) ) = 0
   then Memo1.Lines.Delete(Q);
 end;

Kind regards,
Evarest
0
 
Wim ten BrinkConnect With a Mentor Self-employed developerCommented:
What's so difficult about that? Dump it in a stringlist, start removing lines from the last line upwards to 0 where the position of 'http://' cannot be found at position 1 of the line... Since you already have everything in a TMemo, use the TMemo.Lines property.

const http = 'HTTP://';
var I:Integer;
begin
  for I := Pred(YourMemo.Lines.Count) downto 0 do
    if not AnsiSameText(copy(YourMemo.Lines[I], 1, leength(http), http) then
      YourMemo.Lines.Delete(I);
end;

Something like that?
Just use the Delphi string functions on the Memo.Lines[Index] strings. And remember, if you're going to remove lines, start looping through the list from the last line down to 0.
0
 
Wim ten BrinkSelf-employed developerCommented:
leength=length. Sorry, typo...
0
 
Pierre CorneliusConnect With a Mentor Commented:
Here's a easy all in one solution:

procedure CleanList(var m: TMemo; Exclusions: TStrings);
  function DeleteLine(s: string):boolean;
  var i: integer;
  begin
    result:= Uppercase(copy(m.Lines[i],1, 7)) <> 'HTTP://';
    if not result then
    for i:= 0 to Exclusions.Count-1 do
      if (copy(s,1, Length(Exclusions[i])) = Exclusions[i]) then
        begin
          result:= true;
          break;
        end;
  end;
var i,j: integer;
begin
  i:= 0;
  while i < m.Lines.Count do
  begin
    if DeleteLine(m.Lines[i])
      then m.Lines.Delete(i)
      else i:= i+1;
  end;
end;

USAGE: I had 2 memo's on the form
=====

procedure TForm1.Button1Click(Sender: TObject);
begin
  CleanList(memo1, memo2.Lines);
end;

Regards
Pierre
0
 
Wayne BarronAuthor, Web DeveloperAuthor Commented:
Thank you everyone;

  I split the points to everyone. But for the easiest and quickiest
Solution I gave [Evarest] 75 Points for his solution.

The other codes may come in handy for "Learning Purposes"
For me and others with the same issue's.

Thank you all once again.
very fast answers.

carrzkiss
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.