在某些单词vb.net之后或之前剥离字符串

时间:2013-06-17 16:37:22

标签: vb.net string cut

我想要剪切一个字符串并取出某个单词之前的内容以及某个单词之后的内容。

示例:

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中执行此操作?

2 个答案:

答案 0 :(得分:6)

您可以使用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)

答案 1 :(得分:4)

您可以使用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)