加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

获取其他字符串vb.net之间的字符串

发布时间:2020-12-17 07:18:34 所属栏目:百科 来源:网络整理
导读:我有下面的代码.如何在括号内输入字符串?谢谢. Dim tmpStr() As String Dim strSplit() As String Dim strReal As String Dim i As Integer strWord = "hello (string1) there how (string2) are you?" strSplit = Split(strWord,"(") strReal = strSplit(L
我有下面的代码.如何在括号内输入字符串?谢谢.

Dim tmpStr() As String
    Dim strSplit() As String
    Dim strReal As String
    Dim i As Integer

    strWord = "hello (string1) there how (string2) are you?"

    strSplit = Split(strWord,"(")
    strReal = strSplit(LBound(strSplit))

    For i = 1 To UBound(strSplit)
        tmpStr = Split(strSplit(i),")")
        strReal = strReal & tmpStr(UBound(tmpStr))
    Next

解决方法

Dim src As String = "hello (string1) there how (string2) are you?"
Dim strs As New List(Of String)

Dim start As Integer = 0
Dim [end] As Integer = 0

While start < src.Length

    start = src.IndexOf("("c,start)
    If start <> -1 Then
        [end] = src.IndexOf(")"c,start)
        If [end] <> -1 Then
            Dim subStr As String = src.Substring(start + 1,[end] - start - 1)
            If Not subStr.StartsWith("(") Then strs.Add(src.Substring(start + 1,[end] - start - 1))
        End If
    Else
        Exit While
    End If

    start += 1 ' Increment start to skip to next (

End While

这应该做到这一点.

Dim result = Regex.Matches(src,"(([^()]*))").Cast(Of Match)().Select(Function(x) x.Groups(1))

也会有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读