在vb.net中为firstname和lastname拆分字符串

时间:2010-07-27 18:13:40

标签: vb.net string split

我有一个字符串,上面写着“Joesph Van Andrews”。 我想以这样的方式分割它,名字是“约瑟夫”,姓是“范安德鲁斯” 我怎么能在vb.net中做到这一点?

2 个答案:

答案 0 :(得分:7)

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

假设:名字和姓氏用空格分隔,如果有多个空格,则第一个空格用作分隔符。

答案 1 :(得分:0)

' 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)

只需注意,只有当你想要获取名字中的第一个单词并将其用作名字时才会有效,如果你有一个像Jean Francois Sebastien这样的名字,那么'Jean Francois'将是它的第一个名字。如: 名字:吉恩 姓氏:Francois Sebastien