Solved

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

Posted on 2009-05-04
4
258 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
  • 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now