Solved

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

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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