4

我想剪断一个字符串并获取某个单词之前的内容和某个单词之后的内容。

例子:

Dim string As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"
Dim string_before, string_after As String

--cutting code here--

string_before = "Dr. John "
string_after = " 123 Main Street 12345"

我将如何在 vb.net 中执行此操作?

4

2 回答 2

6

您可以使用String.Split

Dim original As String = "Dr. John Smith 123 Main Street 12345"
Dim cut_at As String = "Smith"

Dim stringSeparators() As String = {cut_at}
Dim split = original.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)

Dim string_before = split(0)
Dim string_after = split(1)
于 2013-06-17T16:42:09.080 回答
5

您可以使用 split() 函数或这个

    Dim mystr As String = "Dr. John Smith 123 Main Street 12345"
    Dim cut_at As String = "Smith"
    Dim x As Integer = InStr(mystr, cut_at)

    Dim string_before As String = mystr.Substring(0, x - 2)
    Dim string_after As String = mystr.Substring(x + cut_at.Length-1)
于 2013-06-17T16:53:31.847 回答