当有多个打开和关闭时,检测属于打开支架的关闭支架。支架内的紧密支架

时间:2016-05-18 03:24:16

标签: vb.net

实际上,我想这样做是为了检测圆角支架内的值并对圆角支架内的任何内容进行舍入。例如:

Dim h As String = "ROUNDING(30.98998(10))*2+3"    
Dim r As String = h.ToString.Substring(h.ToString.IndexOf("ROUNDING(") + 1, h.ToString.IndexOf(")") - 1 - h.ToString.IndexOf("ROUNDING("))

在这种情况下,在ROUNDING(之后还有一个()。如何使ROUNDING上的空心括号与其结尾括号相匹配?

提前致谢!

1 个答案:

答案 0 :(得分:1)

您可以使用以下ReadInBetweenSameDepth Function

Public Function ReadInBetweenSameDepth(str As String, delimiterStart As Char, delimiterEnd As Char) As String
    If delimiterStart = delimiterEnd OrElse String.IsNullOrWhiteSpace(str) OrElse str.Length <= 2 Then
        Return Nothing
    End If
    Dim delimiterStartFound As Integer = 0
    Dim delimiterEndFound As Integer = 0
    Dim posStart As Integer = -1
    For i As Integer = 0 To str.Length - 1
        If str(i) = delimiterStart Then
            If i >= str.Length - 2 Then
                'delimiter start is found in any of the last two characters
                Return Nothing
            End If
            'it means, there isn't anything in between the two
            If delimiterStartFound = 0 Then
                'first time
                posStart = i + 1
            End If
            'assign the starting position only the first time...
                'increase the number of delimiter start count to get the same depth
            delimiterStartFound += 1
        End If
        If str(i) = delimiterEnd Then
            delimiterEndFound += 1
            If delimiterStartFound = delimiterEndFound AndAlso i - posStart > 0 Then
                Return str.Substring(posStart, i - posStart)
                'only successful if both delimiters are found in the same depth
            End If
        End If
    Next
    Return Nothing
End Function

它基本上检查分隔符(例如())是否处于相同的“深度”。