查找并复制从点a到点b到列表的所有子串的出现

时间:2013-06-08 16:37:25

标签: .net vb.net string

我会尝试用伪代码写这个,但我很难自己编写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"

感谢您的时间和精力。

1 个答案:

答案 0 :(得分: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