【发布时间】:2012-12-26 12:39:43
【问题描述】:
我有一个字符串(例如:"Hello there. My name is John. I work very hard. Hello there!"),我正在尝试查找字符串"hello there" 的出现次数。到目前为止,这是我拥有的代码:
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
不幸的是,这行代码似乎是在每次看到phrase 的第一个字母(在本例中为h)时拆分字符串。因此,我实际上得到的不是我希望的结果Occurrences = 2,而是更大的数字。我知道计算字符串中的分割数是一种可怕的方法,即使我确实得到了正确的答案,所以有人可以帮助我并提供一些帮助吗?
【问题讨论】:
-
问题的格式不正确。如果你使用 vb.net 的标签,那么 Split 函数将接受一个字符串,而不仅仅是一个字符。所以在你的情况下,它会是 3,因为你忘了减去 1。参考:msdn.microsoft.com/en-us/library/…
标签: vb.net string split substring