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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5193893/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:01:01  来源:igfitidea点击:

Count specific character occurrences in a string

vb.netstring

提问by Urbycoz

What is the simplest way to count the number of occurrences of a specific character in a string?

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

That is, I need to write a function, countTheCharacters(), so that

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

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

回答by Guffa

The most straightforward is to simply loop through the characters in the string:

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

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

Usage:

用法:

count = CountCharacter(str, "e"C)

Another approach that is almost as effective and gives shorter code is to use LINQ extension methods:

另一种几乎同样有效且代码更短的方法是使用 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

回答by Coyolero

This is the simple way:

这是简单的方法:

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

回答by Mark Harris

You can try this

你可以试试这个

Dim occurCount As Integer = Len(testStr) - Len(testStr.Replace(testCharStr, ""))

回答by MattB

Here's a simple version.

这是一个简单的版本。

text.count(function(x) x = "a")

The above would give you the number of a's in the string. If you wanted to ignore case:

以上将为您提供字符串中 a 的数量。如果您想忽略大小写:

text.count(function(x) Ucase(x) = "A")

Or if you just wanted to count letters:

或者,如果您只想计算字母:

text.count(function(x) Char.IsLetter(x) = True)

Give it a shot!

试一试!

回答by JerryOL

Or (in VB.NET):

或(在 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

回答by Neil Dunlop

Thanks, @guffa. The ability to do it in one line, or even within a longer statement in .NET is very handy. This VB.NET example counts the number of LineFeed characters:

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

Dim j As Integer = MyString.Count(Function(c As Char) c = vbLf)

j returns the number of LineFeeds in MyString.

j 返回 MyString 中 LineFeed 的数量。

回答by Nikc

Conversion of Ujjwal Manandhar's code to VB.NET as follows...

将 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())

回答by Olex White

I suggest you to do it like this:

我建议你这样做:

String.Replace("e", "").Count
String.Replace("t", "").Count

You can also use .Split("e").Count - 1or .Split("t").Count - 1respectively, but it gives wrong values if you, for instance have an e or a t at the beginning of the String.

您也可以分别使用.Split("e").Count - 1.Split("t").Count - 1,但如果您有一个 e 或在String.

回答by Souvik Bose

Public Class VOWELS

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim str1, s, c As String
        Dim i, l As Integer
        str1 = TextBox1.Text
        l = Len(str1)
        c = 0
        i = 0
        Dim intloopIndex As Integer
        For intloopIndex = 1 To l
            s = Mid(str1, intloopIndex, 1)
            If (s = "A" Or s = "a" Or s = "E" Or s = "e" Or s = "I" Or s = "i" Or s = "O" Or s = "o" Or s = "U" Or s = "u") Then
                c = c + 1
            End If
        Next
        MsgBox("No of Vowels: " + c.ToString)
    End Sub
End Class

回答by Andrew'

When I found this solution I was looking for something slightly different as the string I wanted to count was longer than one character, so I came up with this solution:

当我找到这个解决方案时,我正在寻找一些稍微不同的东西,因为我想要计算的字符串比一个字符长,所以我想出了这个解决方案:

    Public Shared Function StrCounter(str As String, CountStr As String) As Integer
        Dim Ctr As Integer = 0
        Dim Ptr As Integer = 1
        While InStr(Ptr, str, CountStr) > 0
            Ptr = InStr(Ptr, str, CountStr) + Len(CountStr)
            Ctr += 1
        End While
        Return Ctr
    End Function