gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:07:12 25 4
gpt4 key购买 nike

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

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 - 获取其他字符串之间的字符串 vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com