Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the number of occurrences of a substring within a string vb.net

I have a string (for example: "Hello there. My name is John. I work very hard. Hello there!") and I am trying to find the number of occurrences of the string "hello there". So far, this is the code I have:

Dim input as String = "Hello there. My name is John. I work very hard. Hello there!"
Dim phrase as String = "hello there"
Dim Occurrences As Integer = 0

If input.toLower.Contains(phrase) = True Then
    Occurrences = input.Split(phrase).Length      
    'REM: Do stuff
End If

Unfortunately, what this line of code seems to do is split the string every time it sees the first letter of phrase, in this case, h. So instead of the result Occurrences = 2 that I would hope for, I actually get a much larger number. I know that counting the number of splits in a string is a horrible way to go about doing this, even if I did get the correct answer, so could someone please help me out and provide some assistance?

like image 461
Matt Avatar asked Jan 11 '13 20:01

Matt


People also ask

How do you find all occurrences of substring in a string?

Use the string. count() Function to Find All Occurrences of a Substring in a String in Python. The string. count() is an in-built function in Python that returns the quantity or number of occurrences of a substring in a given particular string.

How do you find last occurrence of a character in a string in VB net?

LastIndexOf(String, Int32, Int32, StringComparison) Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for the specified number of character positions.

How do you find the number of occurrences of a character in a string in VBA?

Count Occurrences of a Character in a Cell Using VBA in Excel. Using the VBA Replace function with the Len function, we can count the number of occurrences of a character(s) in a cell. The Replace function returns a string after substituting a substring of the string with another substring.

How do you find second occurrence of a character in a string in VB net?

Answers. You've declared charToFind as Char but then pass a String value to the method. If you put Option Strict On at the top of your code you would have an error on your call of GetNthIndex(). I believe this is used with arrays "searchString.


2 Answers

Yet another idea:

Dim input As String = "Hello there. My name is John. I work very hard. Hello there!"
Dim phrase As String = "Hello there"
Dim Occurrences As Integer = (input.Length - input.Replace(phrase, String.Empty).Length) / phrase.Length

You just need to make sure that phrase.Length > 0.

like image 107
Neolisk Avatar answered Sep 28 '22 02:09

Neolisk


the best way to do it is this:

Public Function countString(ByVal inputString As String, ByVal stringToBeSearchedInsideTheInputString as String) As Integer
    Return System.Text.RegularExpressions.Regex.Split(inputString, stringToBeSearchedInsideTheInputString).Length -1

End Function
like image 29
Gaucho Avatar answered Sep 28 '22 00:09

Gaucho