string 如何获得两个其他字符之间的字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4955882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:59:13  来源:igfitidea点击:

How can I get the characters between 2 other characters?

vb.netstring

提问by LiamGu

I have a string which at some point contains the set of characters in the following format [123].

我有一个字符串,它在某些时候包含以下格式的字符集[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?

我将如何在 VB.NET 中解决这个问题?

回答by LukeH

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)

回答by Davide

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

回答by the_lotus

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

回答by JemoGlemo

the statement RegularExpressions.Regex("\[([^\]]*)\]")will returns the value inside bracket Andthe bracket its self!

该语句 RegularExpressions.Regex("\[([^\]]*)\]")将返回括号内的值括号本身!

I have used this statement to return the IPAddress surrounded by () inside long string:-

我已经使用这个语句返回长字符串中由 () 包围的 IPAddress:-

Dim IPRegEx As Regex = New Regex("(?<=\().*?(?=\))")

回答by Mohib Sheth

You could do something like this..

你可以做这样的事情..

Dim val As String = str.Substring(1, str.Length - 2) // replace str with your string variable