[英]Get value in parentheses String VB.NET
我有一個像這樣的字符串:
Dim value as string = "hy the number is (30.01)"
我如何將這個數字30.01轉換為另一個變量? 我正在考慮使用split
,我如何使用它來獲得這個價值,謝謝
使用正則表達式(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
如果您的格式將完全相同,則可以嘗試使用提到的Split。 如果發生任何變化,這可能非常脆弱。
看看這是否適合您。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.