3 Replies - 962 Views - Last Post: 01 September 2010 - 11:52 PM Rate Topic: -----

#1 johnny12358  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-August 10

How do I extract the bracketed part(s) of a string in vb.net

Posted 31 August 2010 - 11:41 AM

Hi,

I am trying to extract the bracketed region of a string into another string. For example

(abc) - (xyz)

How would I extract the 'abc' and 'xyz' into two separate strings?

This is what I have tried to do so far:

  If (operation.Contains("(")) Then
                Dim first = operation.Split("(")
                Dim firsteb = operation.Split(")")




But I then got confused because I could not work out how to avoid it from going to the first and last brackets in the above example

Is This A Good Question/Topic? 0
  • +

Replies To: How do I extract the bracketed part(s) of a string in vb.net

#2 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 463
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: How do I extract the bracketed part(s) of a string in vb.net

Posted 31 August 2010 - 02:18 PM

use indexof and contain functions:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim strTest = "(ABC)-(XYZ)"
        Debug.Print(strTest.IndexOf("-"))
        MessageBox.Show("Right " & strTest.Substring(0, strTest.IndexOf("-")))

    End Sub


Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,913
  • Joined: 25-September 09

Re: How do I extract the bracketed part(s) of a string in vb.net

Posted 01 September 2010 - 05:05 PM

You can split the string and then use .Replace to remove the () from each element of the array.

Here's an example
        Dim operation As String = "(abc)-(xyz)"
        'Split the elements into a string array using - as the token for separation
        Dim elements() As String = operation.Split("-"c)
        'loop through all the elements of the array that were built from the split operation
        For x As Integer = 0 To elements.Count - 1
            'and replace any occurrence of ( with an empty character and ) with an empty character
            elements(x) = elements(x).Replace("(", "").Replace(")", "")
            'Show each element to see if we successfully removed the () for testing purposes
            MessageBox.Show(elements(x))
        Next

Was This Post Helpful? 1
  • +
  • -

#4 Goethals  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 145
  • Joined: 14-April 09

Re: How do I extract the bracketed part(s) of a string in vb.net

Posted 01 September 2010 - 11:52 PM

To get it completely correct:

Iterate trough the characters of your string.
'(' means assign value to StartIndex.
')' means get a substring between StartIndex and me,
and add it to the toreturn list.

Also dont forget to build in a security pattern to see
if bracket structure is correct. (On Init & ')' set startindex -1.
If StartIndex is -1 when getting ')', throw an exception.
If StartIndex <> -1 at end of algoritm, also throw an exception.)

you could also try regular expressions, but they feel awkward to me,
especially of you never used them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1