Solved

If first line of memo starts with either http:// or www. do something?

Posted on 2009-05-04
4
269 Views
Last Modified: 2012-05-06
Hi folks,

The title is quite selfexplanatory but nonetheless, I have this application which extracts certain portions of text between certain tags I fill memo1 with sourcode after which a button press filles memo2 with all the text portions that occur between two defined tags.

Now after this is done I want the app to count the number of links. This is only useful when it contains the links.

I\m planning on using the onChange event but dont know how to code this yet.

Thanks in advance,

Dweeep
0
Comment
Question by:PeterdeB
[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
  • 2
4 Comments
 
LVL 18

Assisted Solution

by:Johnjces
Johnjces earned 100 total points
ID: 24295713
Can you use Pos such as:

of (pos('http://', memo1.lines[0] > 0) or (pos('www', memo1.lines[0] > 0)  then
 ...// do something

This checks just your first line only.


Do a for loop to check other lines.

Hope this helped.

John
0
 
LVL 18

Assisted Solution

by:Johnjces
Johnjces earned 100 total points
ID: 24295732
Again, my keyboard cannot type!

the 'of' is supposed t o be IF.

if (pos.....

John
0
 
LVL 12

Accepted Solution

by:
Hypo earned 400 total points
ID: 24302716
Hi,
Some things you should be aware of is that the Pos function is case sensetive, so while it would detect 'http://' or 'www' in the example above, it would not detect 'HTTP://' or 'WWW'. If you don't want the Pos to be case sensetive, then you would have to convert both strings to capitals using the function UpperCase (one way to do it), e.g. Pos('HTTP://', UpperCase(Memo1.Lines[i]));

Another thing is that the Pos function returns the position of a substring in a string, which means that in the example above, it will detect 'http://' or 'www' in any locations of the strings, so the string ' abc 123 www ' would result in a match as well. If you only want to detect if the string starts with 'http://' or 'www', then you should test that it returns the first position, as in this line "Pos('http://', memo1.lines[i]) = 1". However there are easier ways to do this test.

If you want to test if the string only starts with a text, then you could use AnsiStartsText which exists in the StrUtils unit (in later versions of delphi the function StartsText also exist). The function AnsiStartsText is case insensetive, which means that it would detect 'www' as well as 'WWW'. Also if you want to ignore any white spaces in the start of the text, before you do your string match, then you could use the TrimLeft function on all the lines you test (TrimLeft removes all the whitespace characters in the start of the string), e.g. the reason to use TrimLeft is to detect 'www' as well as '   www' but not 'abc www'.

The example below uses a Memo and a Label on a Form. connect the Memo1Change event to run.

regards
Hypo
procedure TForm1.Memo1Change(Sender: TObject);
var i, j : Integer;
begin
  j := 0;
  for i := 0 to memo1.Lines.Count-1 do
    if (AnsiStartsText('http://', TrimLeft(memo1.lines[i])) or AnsiStartsText('www', TrimLeft(memo1.lines[i])))
      then inc(j); // Do your stuff...
  label1.Caption := IntToStr(j);
end;

Open in new window

0
 

Author Closing Comment

by:PeterdeB
ID: 31577508
Many thanks!

@ Johnjces > your comment would do
@ Hypo > yours is more accurate and complete

That's why I split the points :)

Dweep
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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