0

I want to check whether within particular string exist two brackets (). Starting and ending bracket.

Dim example1 = "1x : (HxBxT) 80x120x44 cm"
Dim example2 = "1x : ( 80x120x44 cm"

In this case example1 contains both brackets: ( and closing one ) therefore it passed the case. In this case i would like to take entire value in this case (HxBxT) and then replace it by something (i will do that). After that i would like insert replace again so for instance at the end i would get this:

Dim example1 = "1x : (whatever i put here) 80x120x44 cm"
2
  • And example2 is invalid so you won't replace it? Commented Dec 21, 2017 at 9:59
  • Use regular expression \(.*\) within Regex class. Commented Dec 21, 2017 at 10:02

4 Answers 4

3

You can use regex:

example1 = Regex.Replace(example1, "\(.*\)", "(whatever i put here)")

If you just want to know if there are opening anc closing brackets you can either use regex:

Dim containsBrackets = Regex.IsMatch(example1, "\(.*\)")

or the VB.NET Like operator:

Dim containsBrackets = example1 like "*(*)*"

or String.IndexOf:

Dim indexOfOpeningBracket =  example1.IndexOf("(")
Dim indexOfClosingBracket =  example1.IndexOf(")", indexOfOpeningBracket + 1)
Dim containsBrackets = indexOfOpeningBracket >= 0 AndAlso indexOfClosingBracket > 0

This also enables you to get the part between the parentheses with Substring:

If containsBrackets
   indexOfOpeningBracket += 1 ' you dont want the parentheses itself but just the content
   Dim partBetween = example1.Substring(indexOfOpeningBracket, indexOfClosingBracket - indexOfOpeningBracket)
End If
Sign up to request clarification or add additional context in comments.

17 Comments

is there a way to firstly check to get yes/no as boolean if () are there and then extract this string and after that do something with it and then replace? More steps but is it possible>
@Tony: i have shown you 3 different ways to get this Boolean. You also want to extract the old substring between the parentheses?
thx and how to get text itself inluding bracets to some variable?
@Tony: then don't add 1 to indexOfOpeningBracket, so remove this line. But add 1 to the length, the second parameter of Substring: example1.Substring(indexOfOpeningBracket, indexOfClosingBracket - indexOfOpeningBracket + 1). Of course you could also use "(" & partBetween & ")"
great and if i want brackets? I will mark as answer.
|
0

In case you don't want to use Regex, you can try this:

Dim str As String = "1x : (HxBxT) 80x120x44 cm"
Dim newValue As String = "whatever i put here"
If str Like "*(*)*" Then
    str = String.Format("{0}{1}{2}",
                        Strings.Left(str, str.IndexOf("("c) + 1),
                        newValue,
                        Strings.Right(str, str.Length - str.IndexOf(")"c)))
End If

Comments

0

Use regular expressions within Regex methods.

Dim input As String = "x : (HxBxT) 80x120x44 cm" 
Dim pattern As String = "\(.*\)"
Dim replacement As String = "(whatever i put here)"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)

Comments

0

you can take help of indexOf method like this

Dim openBracket = myText.IndexOf("(")
Dim closeBracket = myText.IndexOf(")")

if any of openBracket and closeBracket is -1(meaning not found) your case is failed if both are 0 or >0 this means you case pass

then you can use subString method like this to get your string

Dim lenStr = openBracket + closeBracket + 1
dim repStr = myText.Substring(startIndex, lenStr)
dim newString = myText.Replace(repStr,"Whatever text you want to put here)"

I do not have access to VB.net so my syntax may be a little off but this will give you idea

Comments

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.