Don't search for 'cat', but for ' cat ' (space in the beginning and in the end), when in the middle of the sentence. If in the beginning or the end, search for 'cat ' and ' cat' respectively.
Don't search for 'cat', but for ' cat ' (space in the beginning and in the end), when in the middle of the sentence. If in the beginning or the end, search for 'cat ' and ' cat' respectively.
I want number of cat in sentence
EDIT:
Nevermind I figure it out. My brain is still on the beach and my body is in office
B4X:
Sub numbCount
Dim Words() As String
Words = Regex.Split(" ", sentence)
For x = 0 To Words.Length - 1
If Words(x).Contains("cat") Then
numb = numb + 1
Label1.Text = numb
End If
Next
End Sub
First, replace all you spaces with "."
Then replace all your "," with "."
You will then have lots of "cat."
(You will nedd the function Replace(Target, Replacement) for this.
To remove the need of checking for upper or lower cases, use .touppercase
This will make all of your text uppercase.
You will then have lots of "CAT." to search for.
There may be a lot more lean way to do this, but as my head also is in the toilet at the moment, thats my to cents...
Sub numbCount
Dim Words(), TestV As String
Words = Regex.Split(" ", sentence)
For x = 0 To Words.Length - 1
TestV = Words(x).Replace(".", " ") : TestV = TestV.Replace(",", " ") : TestV = TestV.Trim
If TestV.ToLowerCase = "cat" Then
numb = numb + 1
Label1.Text = numb
End If
Next
End Sub
Thank you all for your helps but you all are complicated everything.
Suppose that I need return that string only changed for example into dog.
Don't worry I needed this for my little one project and I know exactly where the cat will show up so don't need to convert spaces into dots or comas into dots...
I was just wondering if margret wrote some short way String Functions just to simplify this.
BTW
Nice String Functions Margret. It help's me alot in my project :sign0098: