son rohan
son rohan

Reputation: 90

Visual Basic: How do I remove parentheses from a string the user enters?

Sample:

The string: George (Babe) Ruth

(calculate) - output:

George Ruth

How would I go about this in VB?

Upvotes: 1

Views: 4581

Answers (3)

Zenacity
Zenacity

Reputation: 296

To piggyback on user2526236's answer....

With VB.net use this function - pass in the string with the parens...

Private Function RemoveChunk(psString As String) As String
        Dim iStart As Integer
        Dim iStop As Integer
        Dim sModifiedString As String

        iStart = InStr(psString, "(")
        iStop = InStr(psString, ")")

        If iStart > 0 And iStop > 0 Then
            sModifiedString = psString.Remove(iStart - 1, iStop + 1 - iStart)
        Else
            'nothing to modify
            sModifiedString = psString
        End If
        Return sModifiedString
    End Function

Upvotes: 0

user2526236
user2526236

Reputation: 1538

It should work as your tag was VB.net.if not the above Regex also works

 Dim s2,s,d,g as string 
 s2 = "George (Babe) Ruth "
 s = InStr(1, s2, " (")
 d = InStr(1, s2, ") ")
 g = s2.Remove(s, d + 1 - s)

Upvotes: 0

Pepe
Pepe

Reputation: 6480

You can use Regex and do this

ResultString = Regex.Replace(SubjectString, "\\(.+\\)", "");

Upvotes: 2

Related Questions