TM80 TM80 - 6 months ago 11
Vb.net Question

VB.net Trying to get text between first and second slashs only

I am trying to retrive the value of the text between the first and second backslashes... but my coding skills have brought me this far and no futher.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim TEST As String = "ONE\TWO\TRHREE\FOR\FIVE"
Dim splitted = TEST.Split("\"c)
Dim values = splitted.Skip(1).Take(splitted.Length - 2).ToArray()
MsgBox(values)
End Sub

Answer

If you want "Two" as result, this should be the simplest approach:

Dim allToken As String() = "ONE\TWO\TRHREE\FOR\FIVE".Split("\"c)
Dim relevantPart = allToken.Skip(1).Take(1)
Dim result As String = String.Concat(relevantPart) ' "Two"

If you don't want a single string but a String() use ToArray:

Dim result As String() = relevantPart.ToArray()

Side-Note: you can't output an array directly, you could use String.Join:

MsgBox(String.Join(", ", result)) ' f.e. comma separated
Comments