Split a String into an array in Swift?

i0S Swift Issue

Question or problem with Swift language programming:

Say I have a string here:

var fullName: String = "First Last"

I want to split the string base on white space and assign the values to their respective variables

var fullNameArr = // something like: fullName.explode(" ") 

var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]

Also, sometimes users might not have a last name.

How to solve the problem:

Solution 1:

The Swift way is to use the global split function, like so:

var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil

with Swift 2

In Swift 2 the use of split becomes a bit more complicated due to the introduction of the internal CharacterView type. This means that String no longer adopts the SequenceType or CollectionType protocols and you must instead use the .characters property to access a CharacterView type representation of a String instance. (Note: CharacterView does adopt SequenceType and CollectionType protocols).

let fullName = "First Last"
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
// or simply:
// let fullNameArr = fullName.characters.split{" "}.map(String.init)

fullNameArr[0] // First
fullNameArr[1] // Last 

Solution 2:

Just call componentsSeparatedByString method on your fullName

import Foundation

var fullName: String = "First Last"
let fullNameArr = fullName.componentsSeparatedByString(" ")

var firstName: String = fullNameArr[0]
var lastName: String = fullNameArr[1]

Update for Swift 3+

import Foundation

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

let name    = fullNameArr[0]
let surname = fullNameArr[1]

Solution 3:

The easiest method to do this is by using componentsSeparatedBy:

For Swift 2:

import Foundation
let fullName : String = "First Last";
let fullNameArr : [String] = fullName.componentsSeparatedByString(" ")

// And then to access the individual words:

var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]

For Swift 3:

import Foundation

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

// And then to access the individual words:

var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]

Solution 4:

Swift Dev. 4.0 (May 24, 2017)

A new function split in Swift 4 (Beta).

import Foundation
let sayHello = "Hello Swift 4 2017";
let result = sayHello.split(separator: " ")
print(result)

Output:

["Hello", "Swift", "4", "2017"]

Accessing values:

print(result[0]) // Hello
print(result[1]) // Swift
print(result[2]) // 4
print(result[3]) // 2017

Xcode 8.1 / Swift 3.0.1

Here is the way multiple delimiters with array.

import Foundation
let mathString: String = "12-37*2/5"
let numbers = mathString.components(separatedBy: ["-", "*", "/"])
print(numbers)

Output:

["12", "37", "2", "5"]

Solution 5:

Swift 4 or later

If you just need to properly format a person name, you can use PersonNameComponentsFormatter.


The PersonNameComponentsFormatter class provides localized
representations of the components of a person’s name, as represented
by a PersonNameComponents object. Use this class to create localized
names when displaying person name information to the user.


// iOS (9.0 and later), macOS (10.11 and later), tvOS (9.0 and later), watchOS (2.0 and later) let nameFormatter = PersonNameComponentsFormatter() let name = "Mr. Steven Paul Jobs Jr." // personNameComponents requires iOS (10.0 and later) if let nameComps = nameFormatter.personNameComponents(from: name) { nameComps.namePrefix // Mr. nameComps.givenName // Steven nameComps.middleName // Paul nameComps.familyName // Jobs nameComps.nameSuffix // Jr. // It can also be configured to format your names // Default (same as medium), short, long or abbreviated nameFormatter.style = .default nameFormatter.string(from: nameComps) // "Steven Jobs" nameFormatter.style = .short nameFormatter.string(from: nameComps) // "Steven" nameFormatter.style = .long nameFormatter.string(from: nameComps) // "Mr. Steven Paul Jobs jr." nameFormatter.style = .abbreviated nameFormatter.string(from: nameComps) // SJ // It can also be use to return an attributed string using annotatedString method nameFormatter.style = .long nameFormatter.annotatedString(from: nameComps) // "Mr. Steven Paul Jobs jr." } 

enter image description here

edit/update:

Swift 5 or later

For just splitting a string by non letter characters we can use the new Character property isLetter:

let fullName = "First Last" let components = fullName.split{ !$0.isLetter } print(components) // "["First", "Last"]\n" 

Hope this helps!