[HELP] Finding Text

Posts 1–7 of 7 · Page 1 of 1
[HELP] Finding Text
I have a variable and like stuff to put into the variable.
But i wanna be able to press a button to see how many of a thing is in the variable.

eg. Variable has "cheesecheesecheese'
i want to be able to search for "cheese" and it will give an integer as an output, in this case 3.

I would like to know how to do this.
Quote Originally Posted by NoobSexPwn View Post
I have a variable and like stuff to put into the variable.
But i wanna be able to press a button to see how many of a thing is in the variable.

eg. Variable has "cheesecheesecheese'
i want to be able to search for "cheese" and it will give an integer as an output, in this case 3.

I would like to know how to do this.
You must use some delimiter between the words or it will become difficult to count. It will require a custom parse for that.

For example, your string should look something like this:

Code:
"Cheese Cheese Cheese"
OR
"Cheese,Cheese,Cheese"
OR
"Cheese
Cheese
Cheese"
Etc, etc..

I've made a quick function for this specific task. Assuming that you are using a delimiter, use the following code to count:

First, Import the following namespace:

Imports System.Text.RegularExpressions

Now add the following function to your program:

Private Function SplitCustom( ByVal wordtosplit As String , ByVal str As String ) As Integer
Dim f () As String = Regex.Split(str, "\W+")
Dim count As Integer = 0
If f .Length > 0 Then
For Each n As String In f
If n = wordtosplit Then
count + = 1
End If
Next
End If
Return count
End Function

For testing, create a button. Double click it and paste the following code in it:

Dim i As String = "cheese,cheese,x"
MsgBox(SplitCustom( "cheese",i))

Now run your program and click the button. It will display total number of 'cheese' words in the string.

Hope this helps !!
ahhh, i can easily put sumthing like a space between them, but i would like to know what the code does, rather than just copy paste because its for an assignment and i'd like to understand so i can learn.
Quote Originally Posted by NoobSexPwn View Post
ahhh, i can easily put sumthing like a space between them, but i would like to know what the code does, rather than just copy paste because its for an assignment and i'd like to understand so i can learn.
Code:
 Private   Function  SplitCustom( ByVal  wordtosplit As   String  , ByVal  str As   String  ) As   Integer
Dim f () As String = Regex.Split(str, "\W+")
Dim count As Integer = 0
If f.Length > 0 Then
For Each n As String In f
If n = wordtosplit Then
count + = 1
End If
Next
End If
Return count
End Function
Code:
Dim f () As String = Regex.Split(str, "\W+")
'This is a regular expression which will split the string in to words. Str is the string the user will input, and "\W+" is the expression used for splitting words.

Code:
Dim count As Integer = 0
Keeps a counter of how many of the required words are found. This variable will be incremented each time the specified word is found.

Code:
If f.Length > 0 Then
For Each n As String In f
If n = wordtosplit Then
count + = 1
End If
Next
End If
First we check that if there is any word splitted or not, by using If statement.
Then we loop through the splitted words via FOR loop.
With in the loop we check that if the current word in the loop is the required word or not. If so, then increment the count variable.


Code:
Return count
At the end of the function we just return the 'count' variable, which contains the total number of words found !!

Still need help, feel free to ask.
oh okay.
i get the 'For Each' bit now, i only ever used 'For'

but i dont understand this part

Code:
Regex.Split(str, "\W+")
i know the str part but what is the bit after.

oh one more thing.
we haven't learned how to import namespaces, how would i go about doing that?
Quote Originally Posted by NoobSexPwn View Post
oh okay.
i get the 'For Each' bit now, i only ever used 'For'

but i dont understand this part

Code:
Regex.Split(str, "\W+")
i know the str part but what is the bit after.

oh one more thing.
we haven't learned how to import namespaces, how would i go about doing that?
"\W+" is a regular expression which splits the words. Just remember that xD.

If you haven't learned then use:

Code:
System.Text.RegularExpressions.Regex.Split(str, "\W+")
Instead of
Code:
Regex.Split(str, "\W+")
Use the "Imports" statement.

In exemplar:

Code:
Imports System.Net
Imports System.Drawing

Public Class Form1

End Class
Posts 1–7 of 7 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us