 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alistair George Guest
|
Posted: Thu Feb 24, 2005 7:30 pm Post subject: Is there a string search character count |
|
|
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
|
Posted: Tue Mar 01, 2005 9:42 am Post subject: Re: Is there a string search character count |
|
|
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
|
Posted: Tue Mar 01, 2005 3:39 pm Post subject: Re: Is there a string search character count |
|
|
"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
|
Posted: Tue Mar 01, 2005 4:28 pm Post subject: Re: Is there a string search character count |
|
|
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 |
|
 |
|
|
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
|
|