4

I have code below. How do I get strings inside brackets? Thank you.

Dim tmpStr() As String
    Dim strSplit() As String
    Dim strReal As String
    Dim i As Integer

    strWord = "hello (string1) there how (string2) are you?"

    strSplit = Split(strWord, "(")
    strReal = strSplit(LBound(strSplit))

    For i = 1 To UBound(strSplit)
        tmpStr = Split(strSplit(i), ")")
        strReal = strReal & tmpStr(UBound(tmpStr))
    Next

4 Answers 4

3
Dim src As String = "hello (string1) there how (string2) are you?"
Dim strs As New List(Of String)

Dim start As Integer = 0
Dim [end] As Integer = 0

While start < src.Length

    start = src.IndexOf("("c, start)
    If start <> -1 Then
        [end] = src.IndexOf(")"c, start)
        If [end] <> -1 Then
            Dim subStr As String = src.Substring(start + 1, [end] - start - 1)
            If Not subStr.StartsWith("(") Then strs.Add(src.Substring(start + 1, [end] - start - 1))
        End If
    Else
        Exit While
    End If

    start += 1 ' Increment start to skip to next (

End While

This should do it.

Dim result = Regex.Matches(src, "\(([^()]*)\)").Cast(Of Match)().Select(Function(x) x.Groups(1))

Would also work.

Sign up to request clarification or add additional context in comments.

2 Comments

perfect :) Thanks a lot. this forum is so helpful and fast :)
what if I have string:" hello ((string1)) there how ((string2)) are you?" - As one char, such as '(' might appear accidentally.
3

This is what regular expressions are for. Learn them, love them:

' Imports System.Text.RegularExpressions
Dim matches = Regex.Matches(input, "\(([^)]*)\)").Cast(of Match)()
Dim result = matches.Select(Function (x) x.Groups(1))

Two lines of code instead of more than 10.

In the words of Stephan Lavavej: “Even intricate regular expressions are easier to understand and modify than equivalent code.”

Comments

1
  1. Use String.IndexOf to get the position of the first opening bracket (x).

  2. Use IndexOf again the get the position of the first closing bracket (y).

  3. Use String.Substring to get the text based on the positions from x and y.

  4. Remove beginning of string up to y+1.

  5. Loop as required

That should get you going.

1 Comment

Thomas and JMK have posted complete answers already using this method. Too slow!
0

This may also work:

Dim myString As String = "Hello (FooBar) World"
Dim finalString As String = myString.Substring(myString.IndexOf("("), (myString.LastIndexOf(")") - myString.IndexOf("(")) + 1)

Also 2 lines.

2 Comments

Zawinski is simply wrong here. People are too quick to resort to this quote. Just because it’s funny doesn’t mean it has any truth to it. And, yes, your code is also in two lines. But is it clear code? I’d argue that it’s not clear, and that the solution using regular expressions is vastly more readable and more maintainable.
Fair enough, removed the quote, and have taken your comments into consideration!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.