BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Is there a string search character count

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Alistair George
Guest





PostPosted: Thu Feb 24, 2005 7:30 pm    Post subject: Is there a string search character count Reply with quote



In a string I want to count how many times a certain character appears.
I have a feeling there is a pascal call for this, but cant recall.
What I've done is:

var spos: integer;
modStr: string;
begin
Spos := pos('@', ComboBox2.text);
if Spos = 0 then exit;
ModStr := copy(ComboBox2.text, Spos + 1, Length(ComboBox2.text));
if pos('@', ModStr) > 0 then //we have more than one occurrence of '@'



http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Back to top
xlo
Guest





PostPosted: Tue Mar 01, 2005 9:42 am    Post subject: Re: Is there a string search character count Reply with quote



Alistair George wrote:

Quote:
In a string I want to count how many times a certain character appears.
I have a feeling there is a pascal call for this, but cant recall.
What I've done is:

var spos: integer;
modStr: string;
begin
Spos := pos('@', ComboBox2.text);
if Spos = 0 then exit;
ModStr := copy(ComboBox2.text, Spos + 1, Length(ComboBox2.text));
if pos('@', ModStr) > 0 then //we have more than one occurrence of '@'


Not sure if this helps - can find one or more character length
substrings. (Omit LowerCase() functions if not needed)
Counts number of occurances of substr in str.

var
i : integer;
str, substr : string;
begin
i := 0;
str := LowerCase(ComboBox1.text);
substr := LowerCase(edit1.text); //substring to find
while pos(substr,str) > 0 do begin
delete(str,pos(substr,str),length(substr)) ;
i := i + 1;
end;
showmessage(Edit1.text + ' was found ' + inttostr(i) + ' times')

end;

Back to top
Bruce Roberts
Guest





PostPosted: Tue Mar 01, 2005 3:39 pm    Post subject: Re: Is there a string search character count Reply with quote




"xlo" <checkthismail (AT) dodgeit (DOT) com> wrote


Quote:
var
i : integer;
str, substr : string;
begin
i := 0;
str := LowerCase(ComboBox1.text);
substr := LowerCase(edit1.text); //substring to find
while pos(substr,str) > 0 do begin
delete(str,pos(substr,str),length(substr)) ;
i := i + 1;
end;
showmessage(Edit1.text + ' was found ' + inttostr(i) + ' times')
end;

If one wants to avoid the creation of temporary strings something like the
following can be used.

function CountOccurrences (const aPattern, aString : string) : integer;

var pC : pChar;

begin
result := 0;
pC := StrPos (pChar (aString), pChar (aPattern));
while (pC <> nil) do
begin
inc (result);
inc (pC, Length (aPattern));
pC := StrPos (pC, pChar (aPattern));
end;
end;





Back to top
J French
Guest





PostPosted: Tue Mar 01, 2005 4:28 pm    Post subject: Re: Is there a string search character count Reply with quote

On Tue, 1 Mar 2005 10:39:55 -0500, "Bruce Roberts"
<ber (AT) bounceitattcanada (DOT) xnet> wrote:

<snip>

Quote:

If one wants to avoid the creation of temporary strings something like the
following can be used.

function CountOccurrences (const aPattern, aString : string) : integer;

var pC : pChar;

begin
result := 0;
pC := StrPos (pChar (aString), pChar (aPattern));
while (pC <> nil) do
begin
inc (result);
inc (pC, Length (aPattern));
pC := StrPos (pC, pChar (aPattern));
end;
end;

In later versions of Delphi there is (I believe) PosEx()

However a Delphi version of InStr() is quite easy to code and
incredibly useful.

http://tinyurl.com/5lmdm



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.