By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,705 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,705 IT Pros & Developers. It's quick & easy.

Counting the periods ( "." ) in a large block of text???

P: n/a
MLH
I have a block of text with about 19,000 characters - alphanumeric,
punctuation, hard returns, etc... I would like to count the number of
periods ( Chr$(46) ) appearing in the document. Whats a good little
code snippet to do something like that?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 11 Aug 2004 10:51:47 -0400, MLH wrote:
I have a block of text with about 19,000 characters - alphanumeric,
punctuation, hard returns, etc... I would like to count the number of
periods ( Chr$(46) ) appearing in the document. Whats a good little
code snippet to do something like that?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


Paste the following Function into a Module.

Public Function CountOccurrences(strFull As String, strSearch As
String) As Integer
' This function will find how many occurrences there are
' of any string within a string.

Dim intX As Integer
Dim intY As Integer
intX = InStr(1, strFull, strSearch)
Do While intX <> 0
intY = intY + 1
intX = InStr(intX + 1, strFull, strSearch)
Loop

CountOccurrences = intY

End Function
================

Then call the function from a query:
Exp: CountOccurrences([FieldName],".")

Or from an Unbound control on a Form or report:
= CountOccurrences([FieldName],".")

You can use it to count any character or string within the string by
just changing CountOccurrences([FieldName],".") to
CountOccurrences([FieldName],"the string")
--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.
Nov 13 '05 #2

P: n/a
Another option to Fred's is

Private Function CountInstances( _
ByVal ToSearch As String, _
ByVal ToFind As String) As Long

CountInstances = (Len(ToSearch) - _
Len(Replace$(ToSearch, ToFind, vbNullString))) _
\ Len(ToFind)

End Function

What does is remove what's you're trying to count from a copy of the string,
and checks how much shorter the string is.
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"MLH" <CR**@NorthState.net> wrote in message
news:oe********************************@4ax.com...
I have a block of text with about 19,000 characters - alphanumeric,
punctuation, hard returns, etc... I would like to count the number of
periods ( Chr$(46) ) appearing in the document. Whats a good little
code snippet to do something like that?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Nov 13 '05 #3

P: n/a
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<cU**********************@twister01.bloor.is. net.cable.rogers.com>...
Another option to Fred's is

Private Function CountInstances( _
ByVal ToSearch As String, _
ByVal ToFind As String) As Long

CountInstances = (Len(ToSearch) - _
Len(Replace$(ToSearch, ToFind, vbNullString))) _
\ Len(ToFind)

End Function

What does is remove what's you're trying to count from a copy of the string,
and checks how much shorter the string is.
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"MLH" <CR**@NorthState.net> wrote in message
news:oe********************************@4ax.com...
I have a block of text with about 19,000 characters - alphanumeric,
punctuation, hard returns, etc... I would like to count the number of
periods ( Chr$(46) ) appearing in the document. Whats a good little
code snippet to do something like that?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


MLH:

Would you do me a favor by trying both functions on your data and
letting me know if there is a significant difference in execution
time.

Thanks in advance,
James A. Fortune
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.