Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Specific Characters in String

Status
Not open for further replies.

aggieval

Programmer
May 23, 2002
25
0
0
US
Does anyone know of a way to count the number of times a specific character appears in a string. I am looking for the character '&' in a field and want to know how many times it appears. Any suggestions would be great.

Thank, Tracy
 
Use:
InStr([fldXXX],"&")
in your qry,

TomCologne
 
Sorry, that will only return the position.

TomCologne
 
This function will count the number of a particular char in the passed string starting at the passed position.

'vintPos = char to start from
'vstrString = string to search
'vstrChar = char to count

Public Function mCountChars(ByVal vintPos As Integer, ByVal vstrString As String, ByVal vstrChar As String) As Integer

Do While InStr(vintPos, vstrString, vstrChar) > 0 And vintPos <= Len(vstrString)
mCountChars = mCountChars + 1
vintPos = (InStr(vintPos, vstrString, vstrChar) + 1)
Loop

End Function

'Called like this

Public Sub mTest()
MsgBox mCountChars(1, &quot;&asddss&asdssd&&quot;, &quot;s&quot;)
End Sub

There are two ways to write error-free programs; only the third one works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top