Stutter
Stutter

Reputation: 1

Taking a String and Removing text inbetween brackets

I'm trying to make a simple text converter program for my first Visual Basic program. I've already written this in Python but I'm not sure how to do it in Visual Basic.

I need the program to run through the characters of a string and once it encounters a ( to then remove the bracket and ignore the rest of the text until it encounters a ).

For Example, "This is a (not so good) sentence", becomes "This is a Sentence".

Previously I did this with a for loop what looked at a character in the string, then checked if it was open. If it wasn't it would append the character to an output string to the next character or if it was it would then trigger a Boolean to true what would stop the character being appended. It would then continue to stop the future characters from being appended until it found a close bracket. At that point, it would then make the Boolean false, stop the closed bracket from being appended and move back to appending characters, unless it was another bracket.

Sorry if this description is a bit rough as I'm not the best at describing things and I'm very new to visual basic. Thanks for any help given

Upvotes: 0

Views: 707

Answers (1)

Ryan Wildry
Ryan Wildry

Reputation: 5677

You can achieve this by several different methods. Here is one method using Instr. Instr returns the character position (index) of a given string in another string. You can use this to determine the bounds of where to include/exclude the string chunk.

You didn't specify if there could be multiple sections encapsulated in () so I assumed there wouldn't be. However, this is a relatively easy tweak by adding either a Do...Loop or a While... loop in the Function.

Hope it helps:

Option Explicit

Public Function removeBrackets(Source As String, Optional RemoveDoubleSpaces As Boolean = False)
    Dim FirstBracket    As Long
    Dim SecondBracket   As Long

    FirstBracket = InStr(1, Source, "(")
    SecondBracket = InStr(1, Source, ")")

    If FirstBracket >= SecondBracket Or FirstBracket = 0 Or SecondBracket = 0 Then Exit Function

    removeBrackets = Left$(Source, FirstBracket - 1) & Right$(Source, Len(Source) - SecondBracket)
    If RemoveDoubleSpaces Then removeBrackets = Replace$(removeBrackets, "  ", " ")
End Function

'Run this
Sub Test()
    Debug.Print "The value returned is: " & removeBrackets("This is a (not so good) sentence") ' Example given
    Debug.Print "The value returned is: " & removeBrackets("This is a (not so good) sentence", True) ' Example given, slight revision. Remove double spaces
    Debug.Print "The value returned is: " & removeBrackets("This is a (not so good sentence") ' missing ending bracket
    Debug.Print "The value returned is: " & removeBrackets("This is a not so good) sentence") ' missing starting bracket
    Debug.Print "The value returned is: " & removeBrackets("This is a not so good sentence") ' No brackets
End Sub

Upvotes: 1

Related Questions