Using Yield to get word count in a string

The below code shows one of the ideas to count the exist of a word in a string

  Private Iterator Function GetWordCount(value As String, s As String) As IEnumerable(Of Integer)
        Dim index As Integer = -1
        Do
            index = value.Split(" ").ToList.IndexOf(s, index + 1)
            If index >= 0 Then
                Yield index
            End If
        Loop While index >= 0
        Return
    End Function

Uage: 

        Dim s As String = "my doc my comp my dog my heart"
        Dim result As String = ""
        result += String.Format("{0}", GetWordCount(s, "my").Count)



Comments

Popular posts from this blog

مقدمة الي تشفير الحروف الأبجدية العربية

VB.NET Translucent Control using GDI+

Add Custom Event to a Class in VB.NET