[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

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
0
Wayne Barron
Asked:
Wayne Barron
3 Solutions
 
EvarestCommented:
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 BrinkCommented:
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 BrinkCommented:
leength=length. Sorry, typo...
0
 
Pierre CorneliusCommented:
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 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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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