| View previous topic :: View next topic |
| Author |
Message |
Alistair George Guest
|
Posted: Thu Feb 24, 2005 7:33 pm Post subject: String search for character |
|
|
In a string I want to find if a character is repeated (or even count the
character). Sure one could iterate through the string, but I have a
feeling there is a pascal call for this, but cant recall; is there?
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 |
|
 |
alanglloyd@aol.com Guest
|
Posted: Thu Feb 24, 2005 8:35 pm Post subject: Re: String search for character |
|
|
I don't think there is a function for this but you can easily write
your own, treating the string as an array of char.
function CountInStr(Ch : char; Str : string) : integer;
var
i : integer;
begin
Result := 0;
for I := 1 to Length(Str) do
if (Str[i] = Ch) then
inc(Result);
end;
Alan Lloyd
[email]alanglloyd (AT) aol (DOT) com[/email]
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Fri Feb 25, 2005 2:22 pm Post subject: Re: String search for character |
|
|
"Alistair George" <noname (AT) xtra (DOT) co.nz> wrote
| Quote: | In a string I want to find if a character is repeated (or even count the
character). Sure one could iterate through the string, but I have a
feeling there is a pascal call for this, but cant recall; is there?
|
function CountChars (const aString : string; aChar : char) : integer;
begin
result := Length (aString) - Length (StringReplace (aString, aChar, '',
[rfReplaceAll]));
end;
|
|
| Back to top |
|
 |
alanglloyd@aol.com Guest
|
Posted: Fri Feb 25, 2005 3:00 pm Post subject: Re: String search for character |
|
|
Bruce Roberts wrote:
| Quote: | function CountChars (const aString : string; aChar : char) : integer;
begin
result := Length (aString) - Length (StringReplace (aString, aChar,
'',
[rfReplaceAll]));
end;
|
Oh - very cunning <g>
Alan Lloyd
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Fri Feb 25, 2005 5:43 pm Post subject: Re: String search for character |
|
|
<alanglloyd (AT) aol (DOT) com> wrote
I found it amusing. Although I suspect that its more expensive than using a
simple For loop.
|
|
| Back to top |
|
 |
J French Guest
|
Posted: Sat Feb 26, 2005 9:08 am Post subject: Re: String search for character |
|
|
On Fri, 25 Feb 2005 12:43:55 -0500, "Bruce Roberts"
<ber (AT) bounceitattcanada (DOT) xnet> wrote:
| Quote: |
[email]alanglloyd (AT) aol (DOT) com[/email]> wrote in message
news:1109343638.182562.257660 (AT) z14g2000cwz (DOT) googlegroups.com...
Oh - very cunning
I found it amusing. Although I suspect that its more expensive than using a
simple For loop.
|
You bet it is - it is doing a monstrous amount of memory moving
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Sat Feb 26, 2005 10:28 am Post subject: Re: String search for character |
|
|
"J French" <erewhon (AT) nowhere (DOT) uk> wrote
| Quote: | On Fri, 25 Feb 2005 12:43:55 -0500, "Bruce Roberts"
[email]ber (AT) bounceitattcanada (DOT) xnet[/email]> wrote:
[email]alanglloyd (AT) aol (DOT) com[/email]> wrote in message
news:1109343638.182562.257660 (AT) z14g2000cwz (DOT) googlegroups.com...
Oh - very cunning
I found it amusing. Although I suspect that its more expensive
than using a simple For loop.
You bet it is - it is doing a monstrous amount of memory moving
|
Which takes all of half a microsecond. You need to save an awful
lot of half microseconds to make up for the minutes saved in
writing the code.
After that come the hours and days spent maintaining the code.
I find it not unusual to sit for two or three hours reading through
existing code before making a single line bugfix. The hours are
spent finding out the exact place to make that small change, and
making sure it _is_ the right place; the big gain to be had is when
that place is so obvious that you can go to it at once. That's
where a good design really pays off.
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
Michael Brown Guest
|
Posted: Sat Feb 26, 2005 2:43 pm Post subject: Re: String search for character |
|
|
Maarten Wiltink wrote:
| Quote: | "J French" <erewhon (AT) nowhere (DOT) uk> wrote in message
news:42203b0d.158257379 (AT) news (DOT) btclick.com...
On Fri, 25 Feb 2005 12:43:55 -0500, "Bruce Roberts"
[email]ber (AT) bounceitattcanada (DOT) xnet[/email]> wrote:
[email]alanglloyd (AT) aol (DOT) com[/email]> wrote in message
news:1109343638.182562.257660 (AT) z14g2000cwz (DOT) googlegroups.com...
Oh - very cunning
I found it amusing. Although I suspect that its more expensive
than using a simple For loop.
You bet it is - it is doing a monstrous amount of memory moving
Which takes all of half a microsecond.
|
Think possibly up nearly 6 orders of magnitude Counting the number of
spaces in the XP Pro EULA (a measly 29kb) takes nearly a second on my 2GHz
Athlon-XP (Barton) machine. Sure, it's fine for very small strings, but why
use a quadratic-complexity algorithm when a linear-time one is trivial to
code?
[...]
--
Michael Brown
www.emboss.co.nz : OOS/RSI software and more
Add michael@ to emboss.co.nz - My inbox is always open
|
|
| Back to top |
|
 |
alanglloyd@aol.com Guest
|
Posted: Sat Feb 26, 2005 4:10 pm Post subject: Re: String search for character |
|
|
Oh - Come _on_ - Let's not be anally-retentive about this <g>. Bruce
was showing off - we all like to do it, and the better (and more
obvious) way had been posted.
Alan Lloyd
|
|
| Back to top |
|
 |
J French Guest
|
Posted: Sat Feb 26, 2005 5:12 pm Post subject: Re: String search for character |
|
|
On Sat, 26 Feb 2005 11:28:42 +0100, "Maarten Wiltink"
<maarten (AT) kittensandcats (DOT) net> wrote:
<snip>
| Quote: | You bet it is - it is doing a monstrous amount of memory moving
Which takes all of half a microsecond. You need to save an awful
lot of half microseconds to make up for the minutes saved in
writing the code.
After that come the hours and days spent maintaining the code.
I find it not unusual to sit for two or three hours reading through
existing code before making a single line bugfix. The hours are
spent finding out the exact place to make that small change, and
making sure it _is_ the right place; the big gain to be had is when
that place is so obvious that you can go to it at once. That's
where a good design really pays off.
|
It is not so much that
- it is more that it indicates that Alan and Bruce believe in the
fallacy (which I am certain they does not)
- that library routines run faster (and are more efficient) than roll
your own
Mostly one finds that such library code runs off into highly undefined
Win APIs.
It is more that if one wrote it, it will darn well work, but if one
uses things that are slightly undefined, one introduces problems that
are not of one's own making.
Rather a big library of small support routines than App code that
contains clever but dubious one liners.
|
|
| Back to top |
|
 |
Alistair George Guest
|
Posted: Tue Mar 01, 2005 8:35 pm Post subject: Re: String search for character |
|
|
Thanks for the very interesting replies all. An education in itself!
Alistair+
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 |
|
 |
Alistair George Guest
|
Posted: Tue Mar 01, 2005 8:42 pm Post subject: Re: String search for character |
|
|
Thanks for the education guys - appreciated.
Alistair+
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 |
|
 |
|