在其他字符串 vb.net 之间获取字符串 [英] get string between other string vb.net

查看:44
本文介绍了在其他字符串 vb.net 之间获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码.如何在括号内获取字符串?谢谢.

I have code below. How do I get strings inside brackets? Thank you.

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))

也可以.

这篇关于在其他字符串 vb.net 之间获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆