如何用VB.NET获取两个字符串之间的子字符串?
- 内容介绍
- 相关推荐
本文共计233个文字,预计阅读时间需要1分钟。
vbDim tmpStr As StringDim strSplit As StringDim strReal As StringDim i As IntegerstrWord=hello (string1) there how (string2) are you?strSplit=Split(strWord, ( )
我有下面的代码.如何在括号内输入字符串?谢谢.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))
也会有用.
本文共计233个文字,预计阅读时间需要1分钟。
vbDim tmpStr As StringDim strSplit As StringDim strReal As StringDim i As IntegerstrWord=hello (string1) there how (string2) are you?strSplit=Split(strWord, ( )
我有下面的代码.如何在括号内输入字符串?谢谢.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))
也会有用.

