计算字符串中的特定字符出现次数

Urb*_*coz 60 vb.net string

计算字符串中特定字符出现次数的最简单方法是什么?

也就是说,我需要编写一个函数countTheCharacters(),这样

str = "the little red hen"
count = countTheCharacters(str,"e") ' Count should equal 4
count = countTheCharacters(str,"t") ' Count should equal 3
Run Code Online (Sandbox Code Playgroud)

Guf*_*ffa 66

最直接的是简单地遍历字符串中的字符:

Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
  Dim cnt As Integer = 0
  For Each c As Char In value
    If c = ch Then 
      cnt += 1
    End If
  Next
  Return cnt
End Function
Run Code Online (Sandbox Code Playgroud)

用法:

count = CountCharacter(str, "e"C)
Run Code Online (Sandbox Code Playgroud)

另一种几乎同样有效并且代码更短的方法是使用LINQ扩展方法:

Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
  Return value.Count(Function(c As Char) c = ch)
End Function
Run Code Online (Sandbox Code Playgroud)

  • 此逻辑也可用于 VBA:`公共函数 CountCharacter(ByVal value As String, ByVal ch As Char) As Integer Dim cnt As Integer For Each c In value If c = ch Then cnt = cnt + 1 End If Next CountCharacter = cnt 结束函数` (2认同)
  • 答案中的代码和注释中的代码不会粘贴到 VBA 中并在第一次尝试时起作用。这是一些 VBA 代码,以防有人在寻找它: Private Function CountCharacter(WhatToSearch As String, SearchForThis As String) As Long Dim NumberOfHits As Long Dim i As Long For i = 1 To Len(WhatToSearch) If Mid(WhatToSearch, i , 1) = SearchForThis Then NumberOfHits = NumberOfHits + 1 End If Next CountCharacter = NumberOfHits End Function (2认同)

Coy*_*ero 60

这是一种简单的方法:

text = "the little red hen"
count = text.Split("e").Length -1 ' Equals 4
count = text.Split("t").Length -1 ' Equals 3
Run Code Online (Sandbox Code Playgroud)

  • 如果起始字母或/和endletter是要计数的数字,则并不总是给出正确的数字 (2认同)
  • @johnywhy:内存没有立即释放,字符串保留在内存中,直到垃圾收集器可以收集它们.短期对象很常见,垃圾收集器经过优化可以有效地处理它们,但它们仍然会增加分配吞吐量并导致垃圾收集器的更多工作.这是由框架处理的,因此对于所有.NET语言都是如此. (2认同)

小智 31

你可以试试这个

Dim occurCount As Integer = Len(testStr) - Len(testStr.Replace(testCharStr, ""))
Run Code Online (Sandbox Code Playgroud)

  • 将你的答案除以`Len(testCharStr)`和`Ceil`来解决字符串的出现问题. (3认同)

Mat*_*ttB 16

这是一个简单的版本.

text.count(function(x) x = "a")
Run Code Online (Sandbox Code Playgroud)

上面的内容将为您提供字符串中的数字.如果你想忽略大小写:

text.count(function(x) Ucase(x) = "A")
Run Code Online (Sandbox Code Playgroud)

或者,如果你只是想数字:

text.count(function(x) Char.IsLetter(x) = True)
Run Code Online (Sandbox Code Playgroud)

试一试!


Jer*_*yOL 5

或(在 VB.NET 中):

Function InstanceCount(ByVal StringToSearch As String,
                       ByVal StringToFind As String) As Long
    If Len(StringToFind) Then
        InstanceCount = UBound(Split(StringToSearch, StringToFind))
    End If
End Function
Run Code Online (Sandbox Code Playgroud)


小智 5

将 Ujjwal Manandhar 的代码转换为 VB.NET 如下...

Dim a As String = "this is test"
Dim pattern As String = "t"
Dim ex As New System.Text.RegularExpressions.Regex(pattern)
Dim m As System.Text.RegularExpressions.MatchCollection
m = ex.Matches(a)
MsgBox(m.Count.ToString())
Run Code Online (Sandbox Code Playgroud)


Nei*_*lop 5

谢谢,@guffa。能够在一行中完成,甚至在 .NET 中的更长语句中完成,这非常方便。这个 VB.NET 示例计算 LineFeed 字符的数量:

Dim j As Integer = MyString.Count(Function(c As Char) c = vbLf)
Run Code Online (Sandbox Code Playgroud)

j 返回 MyString 中 LineFeed 的数量。