Count Character

  • Thread starter Thread starter bw
  • Start date Start date
B

bw

I simply want to count the number of occurences of a character within a string, but I can't
find a function to do it.

For example, MyString = "ABCDE/FGHIJ"

How many "/" characters are in MyString? Isn't there a function that will do this?

If there is a function to do this, a larger question I have is...how do I find functions that will
do things I want? Is there some method for locating the proper function when you think
one (should) exist?


Thanks,
Bernie
 
Well bw

There isn't built-in function for this metter

Try building your own function on vb

for this see help of InStr

Good luck
 
bw said:
I simply want to count the number of occurences of a character within a string, but I can't
find a function to do it.

For example, MyString = "ABCDE/FGHIJ"

How many "/" characters are in MyString? Isn't there a function that will do this?

If there is a function to do this, a larger question I have is...how do I find functions that will
do things I want? Is there some method for locating the proper function when you think
one (should) exist?

See the InStr(), InStrRev() and Len() functions.

I know of no function that will count occurrences of characters in a string
in VB. You might have to write a function that accepts a string and a search
string returning a number.

The online Visual Basic Help has a page entitled Functions Reference.
Unfortunately, using either the Answer Wizard or the Index tabs and entering
those words does not yield the desired result. If you enter a function into
Answer Wizard (like DateAdd), the Functions Reference page will be in the
list ... most of the time ... well, some of the time. Aw heck, I have no
idea how many times out of 100 it will show up, but it works.

Another approach is to use the Contents tab. Under Visual Basic Language
Reference is a subsection entitled Functions.

Be creative - while it would be nice if there were pre-written functions
available to achieve all our goals, there are at least all the tools
necessary for us to write our own solutions.
 
Thanks for the excellent response, as I can see my frustration was not necessarily of my
own doing.

As you suggested, I shall write my own.

Bernie
 
Assuming you're using Access 2000 or higher, there's a nifty trick you can
use.

Use the Replace function to change each occurrance of the character in
question to a null string (""), and see how much shorter the overall string
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

If you're using Access 97 or earlier, you'll need to write your own version
of Replace. http://www.mvps.org/access/strings/str0004.htm at "The Access
Web" shows one way to do that.
 
Doug,

Another nifty trick is to use the split function and count the number of items
in the array. This also allows you to handle more than one character in the
string. As in How many times does the string "Doug" appear in the text of this
post? Again, this requires Access 2K or later, since the split function doesn't
exist in earlier versions.

Public Function fCountString(strIN, _
Optional strDelimit As String = " ") As Long

Dim vCount As Variant

If Len(strIN & vbNullString) = 0 Then
fCountString = 0
Else
vCount = Split(strIN & vbNullString, _
strDelimit, -1, vbTextCompare)
fCountString = UBound(vCount)
End If

End Function

This returns 0 for nulls and zero length strings.
 
Back
Top