【问题标题】:Extract separate double values from string value visual basic. Net从字符串值 Visual Basic 中提取单独的双精度值。网
【发布时间】:2026-01-24 22:50:01
【问题描述】:

我在这里尝试从这个字符串文本“3.6(2.4 到 4.8)”中分别提取双精度值 3.6、2.4、4.8 - 每个都放入一个单独的文本框中

Dim A as string = textbox1.text ' "3.6 (2.4 to 4.8)" 
Textbox2.text = value1' 3.6
Textbox3.text = value2' 2.4
Textbox4.text = value3' 4.8

提前致谢:)

更新

我搜索并尝试使用此代码

Dim A as string = textbox1.text ' "3.6 (2.4 to 4.8)" 
Dim M as Match = Regex.Match(A, "\d+(?:[.,]\d+) *") 
If M.Success Then
Dim R1 = M.groups(0).value
Textbox2.text = R1 ' 3.6

但我不知道如何提取其他 2 个值

【问题讨论】:

  • 您尝试了哪些方法,但没有成功?
  • Honeyboy Wilson 我搜索并尝试使用此代码 ``` Dim A as string = textbox1.text ' "3.6 (2.4 to 4.8)" Dim M as Match = Regex.Match(A, " \d+(?:[.,]\d+) *") If M.Success Then Dim R1 = M.groups(0).value Textbox2.text = R1 ``` 但我不知道如何提取其他 2价值观

标签: string vb.net double extract


【解决方案1】:

我编写了这段代码,它对我有用:

Dim mc As MatchCollection = Regex.Matches(TextBox1.Text, "\d+(?:[.,]\d+) *")
Dim texts(2) As String ' Specifically assigns for 0, 1, 2 (3 elements)
Dim index As Byte = 0 ' Index increment-er

For Each m As Match In mc
    texts(index) = m.ToString ' Writes texts(0... 1... 2...) with m1, m2, m3
    index += 1
Next

TextBox2.Text = texts(0) ' Output
TextBox3.Text = texts(1)
TextBox4.Text = texts(2)

【讨论】: