How to find Number of Occurences of Slash from a strings
BTW, if you are into performance, the following is 20% faster than using split or replace to determine the count:
Private Function GetCountOfChar( _
ByRef ar_sText As String, _
ByVal a_sChar As String _
) As Integer
Dim l_iIndex As Integer
Dim l_iMax As Integer
Dim l_iLen As Integer
GetCountOfChar = 0
l_iMax = Len(ar_sText)
l_iLen = Len(a_sChar)
For l_iIndex = 1 To l_iMax
If (Mid(ar_sText, l_iIndex, l_iLen) = a_sChar) Then 'found occurrence
GetCountOfChar = GetCountOfChar + 1
If (l_iLen > 1) Then l_iIndex = l_iIndex + (l_iLen - 1) 'if matching more than 1 char, need to move more than one char ahead to continue searching
End If
Next l_iIndex
End Function
Old question, but I thought I would add to the quality of the answer by an answer I found at an excel forum. Apparently the count can also be found using.
count =Len(string)-Len(Replace(string,"/",""))
Full credit for the answer goes to the original author at:http://www.ozgrid.com/forum/showthread.php?t=45651
Function CountOfChar(str as string, character as string) as integer
CountOfChar = UBound(Split(str, character))
End Function
Use the below function, as in count = CountChrInString(yourString, "/")
.
'''
''' Returns the count of the specified character in the specified string.
'''
Public Function CountChrInString(Expression As String, Character As String) As Long
'
' ? CountChrInString("a/b/c", "/")
' 2
' ? CountChrInString("a/b/c", "\")
' 0
' ? CountChrInString("//////", "/")
' 6
' ? CountChrInString(" a / b / c ", "/")
' 2
' ? CountChrInString("a/b/c", " / ")
' 0
'
Dim iResult As Long
Dim sParts() As String
sParts = Split(Expression, Character)
iResult = UBound(sParts, 1)
If (iResult = -1) Then
iResult = 0
End If
CountChrInString = iResult
End Function