We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

counting set words (eg "the") in a visual basic string

Horlock
Horlock Posts: 1,027 Forumite
Is there a way of counting the number of times the word the appears in the sentence out of the frying pan into the fire

I have an input sentence as a string and I just want to count the number of matches, I've tried:



regularexpresions.regex.ismatch(input, "the")

but this only returns true or false



From my reading of the documentation I thought that the following would work

regularexpresions.regex.match(input, "the").Groups.Count

but this doesn't seem to give me anything that resembles what I want



I tried a loop

dim counter as integer = 0
while regularexpresions.regex.ismatch(input, "the") regularexpresions.regex.replace(input, "the", "")
counter = counter+1
end while


but the replaces replaces all matches on the first go so gives me zero for none, but 1 for any other number. So no better than the ismatch above

Help please

Finally I'm not too worried about issues with counts arising from the words there, therefore, other but feel free to include measures of how to deal with these
There is no intelligent life out there ... ask any goldfish!

Comments

  • albertross_2
    albertross_2 Posts: 8,932 Forumite
    try using instr, and substr and len in a loop to count
    Ever get the feeling you are wasting your time? :rolleyes:
  • Horlock
    Horlock Posts: 1,027 Forumite
    You are an absolute gem

    Thanks so much!

    I think that substr may be a thing of the past - I couldn't get it to work in vb .net 2005 anyway - but that didn't matter because the method worked a treat.

    Code below shows my function - probably not very elegant - but it works. Thanks again. I'm really greatful!


    Private Function countsubstring(ByVal bigstring As String, ByVal substring As String)
    Dim counter As Integer = 0
    While RegularExpressions.Regex.IsMatch(bigstring, substring)
    counter = counter + 1
    bigstring = bigstring.Remove(InStr(bigstring, substring) - 1, Len(substring))
    ' MessageBox.Show(bigstring & InStr(bigstring, substring) & Len(substring))
    End While
    Return counter
    End Function
    There is no intelligent life out there ... ask any goldfish!
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.5K Banking & Borrowing
  • 254.2K Reduce Debt & Boost Income
  • 455K Spending & Discounts
  • 246.6K Work, Benefits & Business
  • 602.9K Mortgages, Homes & Bills
  • 178.1K Life & Family
  • 260.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.