I have a string which at some point contains the set of characters in the following format [123]
.
What I would like to do is get the characters between
My solution:
Dim s As String = "nav[1]=root"
dim result as String = s.substring(s.indexof("[")+1, s.indexof("]")-s.indexof("[")-1)
You could do something like this..
Dim val As String = str.Substring(1, str.Length - 2) // replace str with your string variable
Dim s As String = "foo [123]=ro bar"
Dim i As Integer = s.IndexOf("[")
Dim f As String = s.Substring(i + 1, s.IndexOf("]", i + 1) - i - 1)
the statement RegularExpressions.Regex("\[([^\]]*)\]")
will returns the value inside bracket And the bracket its self!
I have used this statement to return the IPAddress surrounded by () inside long string:-
Dim IPRegEx As Regex = New Regex("(?<=\().*?(?=\))")
You could use regular expression.
Dim s, result As String
Dim r As RegularExpressions.Regex
s = "aaa[bbbb]ccc"
r = New RegularExpressions.Regex("\[([^\]]*)\]")
If r.IsMatch(s) Then
result = r.Match(s).Value
Else
result = ""
End If
Dim s As String = "nav[1]=root"
dim result as String = s.Substring(s.IndexOf("[") + 1, s.IndexOf("]", s.IndexOf("[")) - s.IndexOf("[") - 1)