Funtion to count characters in strings

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a function whereby I can count how many times a character or word
appears in a string or memo field?

e.g. functionname("For Forfeiting Friday"),"F") = 3 (assuming case sensitive)
 
If you weren't concerned about case sensitive, you could use:
Len([Your Field]) - Len(Replace([Your Field],"F",""))
 
You can create a function like this:

Public Function ReturnNum(InputString, SrchChar) As Integer

Dim array1 As Variant

array1 = Split(InputString, SrchChar)
ReturnNum = UBound(array1)

End Function

Then you'd ask for ReturnNum("For Forfeiting Friday","F") and get 3.
 
How does your function become case sensitive? There are 4 Fs in the string
including both upper and lower case.

--
Duane Hookom
MS Access MVP

kingston via AccessMonster.com said:
It returns 3 for me. I'm running AC2000 and VB6.3.

Duane said:
Kingston,
Your function returns 4, not 3.
You can create a function like this:
[quoted text clipped - 14 lines]
e.g. functionname("For Forfeiting Friday"),"F") = 3 (assuming case
sensitive)
 
I dunno. It just is. I added a dozen lower case f's and it still returns 3.
Can I send you a screenshot?

Duane said:
How does your function become case sensitive? There are 4 Fs in the string
including both upper and lower case.
It returns 3 for me. I'm running AC2000 and VB6.3.
[quoted text clipped - 6 lines]
 
Actually, since Replace has an optional argument to allow binary comparison
or text comparison, Duane Hookom's example works with the additional
argument specified.

Len([Your Field]) - Len(Replace([Your Field],"F","",,,0)


Duane Hookom said:
If you weren't concerned about case sensitive, you could use:
Len([Your Field]) - Len(Replace([Your Field],"F",""))

--
Duane Hookom
MS Access MVP

Boyd said:
Is there a function whereby I can count how many times a character or
word
appears in a string or memo field?

e.g. functionname("For Forfeiting Friday"),"F") = 3 (assuming case
sensitive)
 
Also, the function Split() can be set to distinguish between lower case and
upper case characters via the flag vbBinaryCompare:

array1 = Split(InputString, SrchChar, , vbBinaryCompare)

Duane said:
How does your function become case sensitive? There are 4 Fs in the string
including both upper and lower case.
It returns 3 for me. I'm running AC2000 and VB6.3.
[quoted text clipped - 6 lines]
 
Ok that explains the difference. John Spencer add to my earlier suggestion
with similar functionality.

--
Duane Hookom
MS Access MVP

kingston via AccessMonster.com said:
Also, the function Split() can be set to distinguish between lower case
and
upper case characters via the flag vbBinaryCompare:

array1 = Split(InputString, SrchChar, , vbBinaryCompare)

Duane said:
How does your function become case sensitive? There are 4 Fs in the string
including both upper and lower case.
It returns 3 for me. I'm running AC2000 and VB6.3.
[quoted text clipped - 6 lines]
e.g. functionname("For Forfeiting Friday"),"F") = 3 (assuming case
sensitive)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top