Private Sub ParseSquareBrackets(Text As String) As String
Dim Level As Int
Dim SB As StringBuilder
SB.Initialize
For i = 0 To Text.Length - 1
Dim ch As Char = Text.CharAt(i)
If ch = "[" Then
Level = Level + 1
If Level = 1 Then
SB.Append("[Plain][")
Else
SB.Append("[")
End If
Else If ch = "]" Then
Level = Level -1
If Level = 0 Then
SB.Append("][/Plain]")
Else
SB.Append("]")
End If
Level = Max(Level,0)
Else
SB.Append(ch)
End If
Next
If Level > 0 Then SB.Append(" [/Plain]")
Return SB.ToString
End Sub