开发者

How can I get the characters between 2 other characters?

开发者 https://www.devze.com 2023-02-09 12:29 出处:网络
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 [] but the characters in between are never the

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 [] but the characters in between are never the same length.

How would I go about 开发者_运维技巧this in VB.NET?


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)


Dim s As String = "nav[1]=root"
dim result as String = s.Substring(s.IndexOf("[") + 1, s.IndexOf("]", s.IndexOf("[")) - s.IndexOf("[") - 1)


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


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("(?<=\().*?(?=\))")


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
0

精彩评论

暂无评论...
验证码 换一张
取 消