VB从文本字段/字符串中获取数字

时间:2015-09-07 10:21:03

标签: vb.net string numbers line

我想在这样的行中提取数字

  

[Helpdesk- TestUser - 10406]注册案例#10405。

假设我想从该行中提取10406,您能定义“]”之前的数字是目标吗?或者在“#”之后,我将如何进行?

2 个答案:

答案 0 :(得分:2)

如果我理解正确,那么可以使用以下解决方案找出数字。

首先导入以下库

Imports System.Text.RegularExpressions

  Dim mytext As String = "[Helpdesk- TestUser - 10406] case # 10405 is registred."
  Dim arr As String() = mytext.Split("]")

  Dim FirstNumber = Regex.Replace(arr(0), "\D", "") 
  ' this will fetch first number i.e 10406

  Dim SecondNumber = Regex.Replace(arr(1), "\D", "") 
  ' this will fetch second number i.e 10405

如果这不是您所寻找的解决方案,请详细说明您的问题或评论

根据OP's comment

以下方法将遍历给定字符串中的所有数字

Dim mytext5 As String = "case 10405 [Helpdesk- TestUser - 10406] case # 10405 is registred."
        Dim arr1 As String() = mytext5.Split(" ")
        For Each itm As String In arr1
            Dim num As Object
            num = Regex.Replace(itm, "[^0-9]", "")
            If IsNumeric(num) Then
                'Your Code
            End If
        Next

答案 1 :(得分:1)

你可以尝试这样的事情......

scanStr = "[Helpdesk- TestUser - 10406] case # 10405 is registred"

StartPos = Instr(scanStr, "#")
EndPos = Instr(scanStr, " in")

strSubstr = Mid$(scanStr, StartPos , (EndPos - StartPos) )