473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5052
Here is a function that you can use:

Public Function CountStringOccu rrence(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

CountStringOccu rrence = 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 CountStringOccu rrence = _
CountStringOccu rrence + 1
Next intC
End Function
--

Ken Snell
<MS ACCESS MVP>

"galsaba" <ga*****@aol.co m> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.co m> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.co m> wrote in news:1115131189 .653532.73100
@z14g2000cwz.go oglegroups.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
CountOccurrence s "My name is John Mmm Smith, " _
& "and my wife is Katy Smith-Smithm", _
"m", Occurs
MsgBox Occurs '10
End Sub

Sub CountOccurrence s( _
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
CountOccurrence s 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********@com pumarc.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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
4566
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 process starts is not especially important, but it must be complete within a small number of seconds. The operations I am performing do not take a long time (hundreds of milliseconds), but as each part of the process is complete, my worker thread...
3
1890
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 Forms): 1) I do not know how to take text fram a text file and paste it into a textBox. I can do it like that: ifstream in("teksttt.txt");
7
3586
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 categories where the selected product belongs to. These checked categories are listed in a textbox named "theIDs" (ex: 25,116,420) In the texbox named "chk_counter" there is the number of selected categories: in this example: 3 So, in the table, I...
3
1410
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 Control library along with 6 other namespaces. My root functions are in this form and all the other namespaces revolve around this form. I add the project as a dll to another windows project and call its functions there. Option Strict is
23
3745
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 application logs data to a Postgres table continuously (once every 15 seconds), maintaining a persistent connection. Each datum is logged with a time stamp (Postgres type "timestamp with time zone"). The application does not explicitly set the...
16
2142
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, chkProduceDealer, txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) " + _ "VALUES ('" + txtName.Text + "','" + txtCompany.Text + "','" + txtPhone.Text + "','" + txtEmail.Text + "','" + txtComment.Text + "','" +
4
2819
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 is the best syntax for this please? Regards
9
2284
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." 4. Display last modified date of doc. Here is my attempt. What a headache :-(
11
5517
!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 chr(0) characters. In fact it is the following string: chr(100) & chr(0) & chr(0) & chr(163) This is a marker in the image file for items I need to process. So I though I would do the following: Dim sFileBuffer as String =...
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.