Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

# of occurences in a string

Status
Not open for further replies.
Jun 24, 2005
340
US
Is there a VB function that counts the number of occurences of a string inside another?

If more than 1 goose are geese, why aren't more than 1 moose meese??
[censored][censored][censored]
 
Here is a way. (Just made it)

Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

        Dim a As String = "me me!@# me"
        Dim f As String = "me"

        MessageBox.Show(String.Format( _
            "'{0}' is contained {1} times in '{2}'.", f, Find(a, f), a))

    End Sub


    Private Function Find(ByVal MyString As String, ByVal WhatToFind As String) As Integer

        Dim times As Integer = 0
        Dim _s As String = MyString

        While _s.IndexOf(WhatToFind) <> -1
            _s = _s.Substring(_s.IndexOf(WhatToFind) + 1)
            times += 1
        End While

        Return times

    End Function

Enjoy!
 
Instead of looping, why not just compare a variable where you've replaced the specified value with a nullstring character and just do the simple math?

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
or

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

        Dim a As String = "me me!@# me"
        Dim f As String = "me"

        MessageBox.Show(String.Format( _
            "'{0}' is contained {1} times in '{2}'.", f, a.Split(f).Length() - 1,a))

    End Sub

so

a.Split(f).Length() - 1

Christiaan Baes
Belgium

"My new site" - Me
 
chrissie1,
I like the variable use in the messagebox. Coming from VB I am still using msgbox.
djj
 
LOL got ya!

How many times is 'me' contained in 'mmemeeme' ?
That's why i did it with the loop.


Regards
 
TipGiver, I still don't understand why you need a loop!!


In each of these two:
TextBox3 is the string to search
TextBox4 is what to search for

A one-liner:

[tt]
MessageBox.Show(System.Text.RegularExpressions.Regex.Matches(TextBox3.Text, TextBox4.Text).Count.ToString)

[/tt]

or a bit neater:

[tt]
Dim rx As New System.Text.RegularExpressions.Regex(TextBox4.Text)
MessageBox.Show(rx.Matches(TextBox3.Text).Count.ToString)
rx = Nothing

[/tt]


The core functionality therefore is either:

System.Text.RegularExpressions.Regex.Matches(<string>, <pattern>).Count

or (where Regex object already exists and has been assigned a pattern):

RegExObject.Matches(<string>).Count


Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top