Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip String after or before certain word vb.net

Tags:

string

vb.net

cut

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?

like image 819
user2494189 Avatar asked Dec 21 '22 04:12

user2494189


2 Answers

You could use 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)
like image 79
Reed Copsey Avatar answered Jan 15 '23 16:01

Reed Copsey


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)
like image 33
matzone Avatar answered Jan 15 '23 15:01

matzone