0

我将尝试用伪代码编写此代码,但我自己正在努力编写 vb.net 代码,因为我对这门语言相当陌生。

我想在字符串中找到点 a 并复制所有字符,直到点 b 结束。

然而,原始字符串包含我想要的“句子”的多次出现,因此我想获得列表中的所有出现。

Dim original ="hello mike bye some words hello kate your nice bye" etc.

Dim list As New List(Of String)
Dim pointA As String ="hello"
Dim pointB As String = "bye"

*Psuedo Code*
While not end of string
dim copy As String
--Find first a
--Find first b
--copy all sentence
--list.Add(copy)
--Find next occurrence

essentially the List would now contain:
list(0) = "hello mike bye"
list(1) = "hello kate your nice bye"

感谢您的时间和精力。

4

1 回答 1

1

使用字符串方法 IndexOf 和 Substring

Dim original ="hello mike bye some words hello kate your nice bye and other strings"
Dim startWord = "hello"
Dim stopWord = "bye"
Dim words = new List(Of String)()

Dim pos1 = original.IndexOf(startWord, StringComparison.CurrentCultureIgnoreCase)
Dim pos2 = original.IndexOf(stopWord, StringComparison.CurrentCultureIgnoreCase)
while pos1 <> -1 AndAlso pos2 <> -1
    words.Add(original.Substring(pos1, pos2 + stopWord.Length - pos1))
    pos1 = original.IndexOf(startWord, pos1 + 1, StringComparison.CurrentCultureIgnoreCase)
    pos2 = original.IndexOf(stopWord, pos2 + 1, StringComparison.CurrentCultureIgnoreCase)
End While

for each s in words
    Console.WriteLine("[" + s + "]")
next
于 2013-06-08T16:50:02.307 回答