473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How many time a string appears in a text

How can I find how many time a string appears in a text.
Is there a function in access that does it?

For example,
text: "My name is John Smith, and my wife is Katy Smith"
HowMany(text, "Smith")
this will give me 2.

Thanks,

galsaba

Nov 13 '05 #1
5 5047
Here is a function that you can use:

Public Function CountStringOccurrence(ByVal sStr As String, ByVal sChar As
String, _
Optional yCase As Boolean = True) As Long
' Ken Snell 3 May 2005
'**THIS FUNCTION COUNTS THE NUMBER OF TIMES A TEXT SUBSTRING IS FOUND IN A
TEXT STRING. THE
'**SEARCH CAN BE CASE-SENSITIVE (yCase is True) OR CASE-INSENSITIVE (yCase
is False).
'**THE FUNCTION'S VALUE IS THE NUMBER OF TIMES THE TEXT SUBSTRING IS FOUND.

' intC is used as a loop counter
' sChar is the text substring for which the search is to be done
' sStr is the text string that is to be searched
' yCase is the "case sensitivity" flag (True for case sensitivity; False for
case
' insensitivity)

Dim intC As Long, intLen As Long

CountStringOccurrence = 0
intLen = Len(sChar)
If yCase = False Then
'user desires case insensitivity, so make everything Upper Case
sStr = UCase(sStr)
sChar = UCase(sChar)
End If
For intC = 1 To Len(sStr)
'if character is found, increment the counter
If Mid(sStr, intC, intLen) = sChar Then CountStringOccurrence = _
CountStringOccurrence + 1
Next intC
End Function
--

Ken Snell
<MS ACCESS MVP>

"galsaba" <ga*****@aol.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
How can I find how many time a string appears in a text.
Is there a function in access that does it?

For example,
text: "My name is John Smith, and my wife is Katy Smith"
HowMany(text, "Smith")
this will give me 2.

Thanks,

galsaba

Nov 13 '05 #2

Function HowMany( _
Optional ByVal String1, _
Optional ByVal String2, _
Optional ByVal Compare As VbCompareMethod = vbBinaryCompare _
) As Integer

Dim intStart As Integer
Dim intCount As Integer
Dim intInstr As Integer
intCount = 0

intStart = 1
intInstr = InStr(intStart, String1, String2, Compare)

Do While intInstr > 0
intCount = intCount + 1
intStart = intInstr + Len(String2)
intInstr = InStr(intStart, String1, String2, Compare)
Loop
HowMany = intCount
End Function
--
Terry Kreft
MVP Microsoft Access
"galsaba" <ga*****@aol.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
How can I find how many time a string appears in a text.
Is there a function in access that does it?

For example,
text: "My name is John Smith, and my wife is Katy Smith"
HowMany(text, "Smith")
this will give me 2.

Thanks,

galsaba

Nov 13 '05 #3
"galsaba" <ga*****@aol.com> wrote in news:1115131189.653532.73100
@z14g2000cwz.googlegroups.com:
How can I find how many time a string appears in a text.
Is there a function in access that does it?

For example,
text: "My name is John Smith, and my wife is Katy Smith"
HowMany(text, "Smith")
this will give me 2.


I think I woud use a recursive sub here. Air code ...

'Option Compare Database
'Option Compare Binary
Option Compare Text
Option Explicit

Sub temp()
Dim Occurs As Long
CountOccurrences "My name is John Mmm Smith, " _
& "and my wife is Katy Smith-Smithm", _
"m", Occurs
MsgBox Occurs '10
End Sub

Sub CountOccurrences( _
ByVal vInWhat As String, _
ByVal vCountWhat As String, _
ByRef rOccurs As Long, _
Optional ByVal vStart As Long)

vStart = InStr(vStart + 1, vInWhat, vCountWhat)
If (vStart = 0) Then
Exit Sub
Else
rOccurs = rOccurs + 1
CountOccurrences vInWhat, vCountWhat, rOccurs, vStart
End If
End Sub

I think I might be in the minority here, though.

--
Lyle

"The aim of those who try to control thought is always the same. They find
one single explanation of the world, one system of thought and action that
will (they believe) cover everything; and then they try to impose that on
all thinking people."
- Gilbert Highet
Nov 13 '05 #4
The most elegant solution, but not the fastest or most efficient:

http://groups-beta.google.com/group/...8bcc0ce3?hl=en

I haven't found the best way to do this.

James A. Fortune

I'm also looking for a "Blue Ocean" strategy for Access.

Nov 13 '05 #5
Yes very nice, but the declaration should be (for most purposes)
Public Function CountSubstrings(ByVal strIn As String, strFind As String) As
Integer
--
Terry Kreft
MVP Microsoft Access

<ji********@compumarc.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
The most elegant solution, but not the fastest or most efficient:

http://groups-beta.google.com/group/...8bcc0ce3?hl=en
I haven't found the best way to do this.

James A. Fortune

I'm also looking for a "Blue Ocean" strategy for Access.

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Charles Law | last post by:
Hi guys I have a time critical process, running on a worker thread. By "time critical", I mean that certain parts of the process must be completed in a specific time frame. The time when the...
3
by: man | last post by:
Hello, At first I have to warn you - I'm a newbie so don't shout me down and don't beat me :). I have a few problems concerning programming using VC++ .NET Std 2003 (particularly Windows...
7
by: nicholas | last post by:
I have a page that changes some data in an sql-server database: it inserts the categories for a selected product. The user checks some categories in a tree (with checkboxes). These are the...
3
by: scorpion53061 | last post by:
I have little hope of resolving this as I have had to contact outside help. But I thought I would post it here to see if anyone could add an idea or solution. 1. I have a form in a Class...
23
by: Randall Nortman | last post by:
I assume I'm not the first person to have encountered this, but I couldn't find anything in the FAQ or on the mailing lists recently. My apologies if this is already documented somewhere... My...
16
by: Mark A. Sam | last post by:
Hello, I am having a problem with imputting into a string variable: Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany, txtPhone, txtEmail, txtComment, chkGrower,...
4
by: Terry | last post by:
I have a TextBox with a date such as 15/01/2006 which I want to cast into a variable as a short date 15/01/06, also I need to cast a time such as 07:30 A.M. into a variable as a short time. What...
9
by: LayneMitch via WebmasterKB.com | last post by:
Hello. Got another one for you folks. I'm working on this problem that wants me to 1. Prompt for name 2. Use pop-up box with name 3. Display current date on page in format "October 30, 2000."...
11
!NoItAll
by: !NoItAll | last post by:
I need to search a buffer for a string of characters. The buffer is essentially a binary file (it's an image) and contains lots of chr(0) characters. The string I need to locate also contains...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.