Warm tip: This article is reproduced from serverfault.com, please click

excel-如何从字符串中查找斜线的出现次数

(excel - How to find Number of Occurences of Slash from a strings)

发布于 2012-02-13 13:02:44

如何使用Excel VBA宏查找字符串中正斜杠字符(/)的出现次数?

Questioner
Code Hungry
Viewed
11
20.8k 2012-02-13 22:09:45

使用以下功能,如中所示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