0

我正在使用 javascript 并想转换为 VB.NET。该代码是关于检查天气括号余额或不在给定字符串中的。

var ncount = 1
var str = "hello(world)"

if (ncount != 0) {
ncount = ncount - 1
//Using for loop to search each char in string
for (???) {
if (//If open parentheses is found)
    ncount++;
else if (//If close parentheses is found)
    ncount--;
else
    //parentheses not balance
}

}else {
// Parentheses balanced
}
4

1 回答 1

0

This should work:

Dim bracketsBalanced = AreBracketsBalanced("hello(world)") ' True

Public Shared Function AreBracketsBalanced(input As String) As Boolean
    Const LeftParenthesis As Char = "("c
    Const RightParenthesis As Char = ")"c
    Dim BracketCount As Int32 = 0

    For Index As Int32 = 0 To input.Length - 1
        Select Case input(Index)
            Case LeftParenthesis
                BracketCount += 1
            Case RightParenthesis
                BracketCount -= 1
        End Select
    Next
    Return BracketCount = 0
End Function
于 2013-09-21T10:34:02.653 回答