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