Split String in VB.net

后端 未结 2 420
独厮守ぢ
独厮守ぢ 2021-01-24 07:29

How do I split a string \"99 Stack Overflow\" into 2 in vb.net

I want the first value to be 99 and the 2nd to be Stack Overflow.

Please help

2条回答
  •  半阙折子戏
    2021-01-24 07:57

    Assuming you mean numbers, then a space, then more text, you could use a regular expression to do that.

    Dim input As String = "99 Stack Overflow"
    Dim re As New Regex("^(\d+) (.+)$")
    Dim m As Match = re.Match(input)
    Dim firstPart As String
    Dim secondPart As String
    If m.Success AndAlso m.Groups.Count = 3 Then
        firstPart = m.Groups(1).ToString()
        secondPart = m.Groups(2).ToString()
    Else
        'Do something useful'
    End If
    

    If you just mean text, a space, and more text, regex is overkill and T.J. Crowder's suggestion is better.

提交回复
热议问题