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?
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With