John Nuñez
John Nuñez

Reputation: 1830

Get value in parentheses String VB.NET

I have an string like this:

Dim value as string = "hy the number is (30.01)"

How i can get this number 30.01 into another variable? I am thinking to use split, How i can use to get this value thanks

Upvotes: 1

Views: 3041

Answers (2)

dan radu
dan radu

Reputation: 2782

Use regular expressions (System.Text.RegularExpressions):

Dim m As Match = Regex.Match(value, "\((?<n>\d+\.\d{2})\)")
If m.Success Then
  Dim n As Decimal = Decimal.Parse(m.Groups("n").Value)
End If

Upvotes: 4

Mark Hall
Mark Hall

Reputation: 54532

If your format is going to be exactly the same, you can try using Split like you mentioned. This can be very fragile if anything changes.

See if this works for you.

Dim result As Double
Dim value As String = "hy the number is (30.01)"
Dim subValue() As String = value.Split("(")
subValue(1) = subValue(1).TrimEnd(")")
If Not Double.TryParse(subValue(1), result) Then
    'Error handling code here
End If

Upvotes: 3

Related Questions