繁体   English   中英

获取括号中的值字符串VB.NET

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM