【问题标题】:split string for firstname and lastname in vb.net在vb.net中拆分名字和姓氏的字符串
【发布时间】:2010-07-27 18:13:40
【问题描述】:

我有一个字符串说“Joesph Van Andrews”。 我想以这样的方式拆分它,名字是“Joseph”,姓氏是“Van Andrews” 我如何在 vb.net 中做到这一点?

【问题讨论】:

  • Mary Ann Smith 是否会被拆分,这样名字是“Mary”,姓氏是“Ann Smith”?

标签: vb.net string split


【解决方案1】:
Dim firstName As String = name.Substring(0,name.IndexOf(" "))
Dim lastName As String = name.Substring(name.IndexOf(" ")+1)

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

【讨论】:

    【解决方案2】:
    ' 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”是它会返回的名字,这只会起作用作为: 名字:让 姓氏:弗朗索瓦·塞巴斯蒂安

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      相关资源
      最近更新 更多