【问题标题】:Swift: Split first name and last name from full name stringSwift:从全名字符串中拆分名字和姓氏
【发布时间】:2017-08-17 03:23:41
【问题描述】:

如何将全名拆分为名字和姓氏。虽然我已经完成了以下代码,但考虑到fullName 的以下值是否有更好的方法?

全名可以具有以下任何值:

let fullName:String! //(No first or last name)
let fullName = "" //(No first or last name)
let fullName = "Micky" //(fName = Micky, no lName)
let fullName = "Micky Arthur" //(fName = Micky, lName = Arther)
let fullName = "Micky Arthur Test" //(fName = Micky, lName = Arther Test)

我的代码:

if let fullName = name{
   let nameParts = fullName(separatedBy: " ")
   if nameParts.count > 0{
      let fName = nameParts[0]
      var lName = ""
      if nameParts.count > 1{
          for i in 1 ..< nameParts.count{
          lName = lName + nameParts[i] + " "
      }
   }
   print("First Name: \(fName) Last Name: \(lName)")
}

【问题讨论】:

  • 只要让 fName = parts.removeFirst(),然后 lName 将是数组的剩余部分,加入了 " "
  • 请记住,像这样拆分名称的任何尝试都不适用于所有情况。只有一部分的名字可能只是一个姓氏。一个名称可能有 3 个以上的部分。名字和姓氏有多个部分。不同语言的规则可能不同。
  • @rmaddy,我明白了。有什么算法吗?
  • 如果你在 iOS10 上,你可以尝试使用 NSPersonNameComponentsFormatter.personNameComponents(from: String)。 developer.apple.com/documentation/foundation/…

标签: ios swift split


【解决方案1】:

尝试使用此代码

    let fullName = "testing abducio medina"
    var components = fullName.components(separatedBy: " ")
    if components.count > 0 {
     let firstName = components.removeFirst()
     let lastName = components.joined(separator: " ")
     debugPrint(firstName)
     debugPrint(lastName)
    } 

【讨论】:

  • 是的,比我的好。 :) 让我们等待更好,否则这是一个公认的答案..
  • @CchanchaRaj 好的,对我来说似乎很公平
【解决方案2】:

怎么样:

struct Name {
    let first: String
    let last: String

    init(first: String, last: String) {
        self.first = first
        self.last = last
    }
}

extension Name {
    init(fullName: String) {
        var names = fullName.components(separatedBy: " ")
        let first = names.removeFirst()
        let last = names.joined(separator: " ")
        self.init(first: first, last: last)
    }
}

extension Name: CustomStringConvertible {
    var description: String { return "\(first) \(last)" }
}

用途(例如游乐场)

func testName(fullName: String) {
    let name = Name(fullName: fullName)
    print(name.first)
    print(name.last)
    print(name)
}

testName(fullName: "Jane Doe Foo")
//prints
// Jane
// Doe Foo
// Jane Doe Foe


testName(fullName: "John Doe")
//prints
// John
// Doe
// Jane Doe

【讨论】:

    【解决方案3】:

    ​​​​解决方案简单,行数更少:

    let firstName = name.components(separatedBy: "")[0]
    let secondName = name.components(separatedBy: "")[1]
    

    【讨论】:

      【解决方案4】:

      我做过类似的事情

      var Name = "first name"
      var fullNameArr = split(fullName) {$0 == " "}
      var firstName: String = fullNameArr[0]
      let middlename: String = fullNameArr.indices.contains(2)? fullNameArr[1] : ""
      if fullNameArr.count > 1 {
           var lastName: String? = fullNameArr.last
      
      }
      

      【讨论】:

        【解决方案5】:

        试试这个:

        func parseName(_ fullName: String) -> (String, String) {
            let components = fullName.characters.split(separator: " ", maxSplits: 1).map(String.init)
            return (components.first ?? "", components.count > 1 ? components.last! :  "")
        }
        let (fName, lName) = parseName("Micky Arthur Test")
        print("First Name: \(fName) Last Name: \(lName)")
        

        【讨论】:

          【解决方案6】:
           func splitContactFullName(name: String?) -> (firstName: String?, lastName: String?) {
              let namePieces = name?.components(separatedBy: " ")
              let firstName = namePieces?[0]
              var lastName = ""
              if (namePieces?.count ?? 0) > 1 {
                  lastName = namePieces?[1] ?? ""
                  for i in 2..<(namePieces?.count ?? 0) {
                      lastName += " \(namePieces?[i] ?? "")"
                  }
              }
              return (firstName, lastName)
          }
          

          【讨论】:

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