-2

I'm writing a app where I need to present the second name of a person on the screen.

The names are always "firstname space lastname" as in:

let str = "Fred Bloggs"

let secondStr = "William Wright"

Can you tell me how to get "Bloggs" out of that first string and "Wright" out of the second string, not knowing the index of Bloggs. All the examples I've seen seem to assume you know the index of the position you want to get a substring from.

Thanks.

rmaddy
  • 307,833
  • 40
  • 508
  • 550
Phil Edge
  • 15
  • 7
  • Sorry. I guess my searches weren't good enough. I didn't find that I guess because of the wording of the question. – Phil Edge Jan 07 '18 at 13:22
  • 1
    Oh thanks for the 2 downvotes on the question. I guess I won't be using stackoverflow any more. – Phil Edge Jan 07 '18 at 18:34

1 Answers1

0
import Foundation

let fullName    = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")

let name    = fullNameArr[0]
let lastName = fullNameArr[1]
Kathiresan Murugan
  • 2,443
  • 3
  • 22
  • 39