How can I find the number of occurrences of the forward slash character ( / ) within a string using an Excel VBA macro?
The count() method returns the number of occurrences of a substring in the given string.
In order to find occurence of each character in a string we can use Map utility of Java.In Map a key could not be duplicate so make each character of string as key of Map and provide initial value corresponding to each key as 1 if this character does not inserted in map before.
Use the below function, as in count = CountChrInString(yourString, "/") .
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
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 If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With