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

后端 未结 12 2015
逝去的感伤
逝去的感伤 2020-12-06 02:14

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

12条回答
  •  天命终不由人
    2020-12-06 02:45

    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.

提交回复
热议问题