acadia
acadia

Reputation: 2333

Split String in VB.net

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

Upvotes: 1

Views: 1815

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1073968

This should do it:

result = yourstring.Split(new Char() { " "c}, 2)

More here. (I think that's how you write a literal Char array in VB.Net; I'm not much of a VB.Net guy, most of what I do in .Net is in C#.

If I'm wrong about how you right literal char arrays and you can't figure it out, you can use a version of it that takes a String instead:

result = yourstring.Split(" ", 2, StringSplitOptions.None)

Details on that one here.

Upvotes: 4

John M Gant
John M Gant

Reputation: 19308

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.

Upvotes: 2

Related Questions