vba 如何从字符串中找到斜线的出现次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9260982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find Number of Occurences of Slash from a strings
提问by Code Hungry
How can I find the number of occurrences of the forward slash character ( / ) within a string using an Excel VBA macro?
如何使用 Excel VBA 宏查找字符串中正斜杠字符 ( / ) 的出现次数?
采纳答案by assylias
Use the below function, as in count = CountChrInString(yourString, "/")
.
使用以下函数,如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
回答by Santhosh Divakar
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.
老问题,但我想我会通过在 excel 论坛上找到的答案来提高答案的质量。显然计数也可以使用。
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
答案完全归功于原作者:http: //www.ozgrid.com/forum/showthread.php?t=45651
回答by maracuja
Function CountOfChar(str as string, character as string) as integer
CountOfChar = UBound(Split(str, character))
End Function
回答by user9338905
BTW, if you are into performance, the following is 20% faster than using split or replace to determine the count:
顺便说一句,如果您关注性能,以下方法比使用拆分或替换来确定计数快 20%:
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
回答by SandPiper
I like Santhosh Divakar's answer, so I expanded on it to account for the possibility when you want to check for more than just a single character by dividing the result by the length of the search characters, like this:
我喜欢 Santhosh Divakar 的回答,所以我扩展了它以解释当您想要通过将结果除以搜索字符的长度来检查多个字符时的可能性,如下所示:
Function Num_Characters_In_String(Input_String As String, Search_Character As String) As Integer
'Returns the number of times a specified character appears in an input string by replacing them with an empty string
' and comparing the two string lengths. The final result is then divided by the length of the Search_Character to
' provide for multiple Search Characters.
Num_Characters_In_String = (Len(Input_String) - Len(Replace(Input_String, Search_Character, ""))) / Len(Search_Character)
End Function
As an example, the result of
例如,结果
Num_Characters_In_String("One/Two/Three/Four//", "//")
gives you 1, because there is only a double slash at the end of the sentence.
给你 1,因为句子末尾只有一个双斜线。
回答by RIck_R
Here's a single line version for use when you don't want to call a separate function. It's just a compressed version of the CountChrInString and some others above.
这是当您不想调用单独的函数时使用的单行版本。它只是 CountChrInString 和上面其他一些的压缩版本。
? UBound(Split("abcabcabc", "cd"), 1)
This will return 0. If you change "cd" to "ab" it returns 3. It also works with variables. Note that if the string being checked (abcabc...) is empty it WILL return -1.
这将返回 0。如果将“cd”更改为“ab”,则返回 3。它也适用于变量。请注意,如果被检查的字符串 (abcabc...) 为空,它将返回 -1。
回答by Shawn Smith
This is an easy solution for VBA Excel Macros.
这是 VBA Excel 宏的简单解决方案。
Function CharCount(str As String, chr As String) As Integer
CharCount = Len(str) - Len(Replace(str, chr, ""))
End Function