Ready, steady… save!
You don’t need a chart to join in. Here are 2 ways to record your savings:
1. Cross it off
Pop a chart into your first post. Saved an amount? Cross it out! Read more »
£0.01 £0.02 £0.03
2. Write it down
Skip the chart and add updates to your first post. Read more »
“Another +£3 saved today. Go me!”
Use +£ before each new saving so it counts towards your total.
📨 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!
counting set words (eg "the") in a visual basic string
Horlock
Posts: 1,027 Forumite
in Techie Stuff
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
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!
0
Comments
-
try using instr, and substr and len in a loop to countEver get the feeling you are wasting your time? :rolleyes:0
-
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 FunctionThere is no intelligent life out there ... ask any goldfish!0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 355.9K Banking & Borrowing
- 255K Reduce Debt & Boost Income
- 456.2K Spending & Discounts
- 248.4K Work, Benefits & Business
- 606.1K Mortgages, Homes & Bills
- 179K Life & Family
- 263.9K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.2K Discuss & Feedback
- 37.7K Read-Only Boards