Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Number of Occurences of Slash from a strings

Tags:

excel

vba

How can I find the number of occurrences of the forward slash character ( / ) within a string using an Excel VBA macro?

like image 790
Code Hungry Avatar asked Feb 13 '12 13:02

Code Hungry


People also ask

How do you find the number of occurrences in a string?

The count() method returns the number of occurrences of a substring in the given string.

How do you count occurrences of each character in a 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.

How do you count the occurrences of a given character in a string in VBA?

Use the below function, as in count = CountChrInString(yourString, "/") .


2 Answers

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

like image 69
Santhosh Divakar Avatar answered Oct 09 '22 06:10

Santhosh Divakar


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 
like image 42
assylias Avatar answered Oct 09 '22 05:10

assylias