开发者

split string for firstname and lastname in vb.net

i have a string that comes in say "Joesph Van Andrews". I want 开发者_如何学Goto split it in such a way that firstname is "Joseph" and lastname is "Van Andrews" how can i do that in vb.net?


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.


' 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜