Search code examples
vb.netstringcut

Strip String after or before certain word vb.net


I want to cut a string and take what is before a certain word and what is after a certain word.

Example:

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"

How would I do this in vb.net?


Solution

  • You can use split() function or this

        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)