Count Parts in Delimited String


Function StringPartCount(strIn As String, strDelimiter As String) As Long
'Assumes 1. always a part between separators and 2. does not end with separator
'Testcases
'?StringPartCount("a_b_c", "_")=3
'?StringPartCount("__", "_")=3
'?StringPartCount("", "_")=0
'?StringPartCount("a", "_")=1
    If Len(strIn) > 0 Then
        StringPartCount = StringDelimiterCount(strIn, strDelimiter) + 1
    End If
End Function

Function StringDelimiterCount(strIn As String, strDelimiter As String) As Long
'?StringDelimiterCount("a_b_c", "_")=2
'?StringDelimiterCount("__", "_")=2
'?StringDelimiterCount("", "_")=0
'?StringDelimiterCount("a", "_")=0
'?StringDelimiterCount("aaa", "aa")=1' ??
  Dim icount  As Long
  Dim i As Long
    i = 1
    While i > 0
        i = InStr(i, strIn, strDelimiter)
        If i > 0 Then icount = icount + 1: i = i + 1
    Wend
    StringDelimiterCount = icount
End Function