views:

68

answers:

2

i have a string that comes in say "Joesph Van Andrews". I want to split it in such a way that firstname is "Joseph" and lastname is "Van Andrews" how can i do that in vb.net?

+1  A: 
Dim firstName As String = name.Substring(0,name.IndexOf(" "))
Dim lastName As String = name.Substring(name.IndexOf(" ")+1)

Assumptions: first name and last name are separated by a space, and if multiple spaces are present, the first space is used as the separator.

dcp
A: 
' We want to get the name and put it in a variable
Dim name As String = "Joseph Van Andrews"

' Split string based on spaces
Dim names As String() = name.Split(New Char() {" "c})

' Seperate the first name from the rest of the string
Dim lastName as string = name.substring(names(0).length())

Dim nameString as string = "the First Name is: " + names(0) + " and the Last Name is: " + lastName

Console.WriteLine(nameString)

Just a note this will only work if you want to grab the first word in the name and use it as the first name, if you have a name like Jean Francois Sebastien and 'Jean Francois' is the first name it will return as: First Name: Jean Last Name: Francois Sebastien

Latency