我正在使用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
}
答案 0 :(得分:0)
这应该有效:
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