Get the first word in a String of words & spaces - Substring first word before space

If your string is heavy, componentsSeparatedByString() tends to be faster.

Swift 2:

var date = "1,340d 1h 15m 52s"
if let first = date.componentsSeparatedByString(" ").first {
    // Do something with the first component.
}

Swift 3/4/5:

if let first = date.components(separatedBy: " ").first {
    // Do something with the first component.
}

In this context, neither the .componentsSeparatedByString(" ") method nor the characters.split(" ") method is the "best approach". Both these methods will traverse the full String object, and give a String array as an result. Only after the array has been computed do we extract the first entry of this array. If we're treating a huge string, this is quite unnecessary in this context, and will result in an unnecessary overhead.

Instead, for a huge string, the following method is to prefer:

let firstDateEntryFast = date.substringToIndex((date.rangeOfString(" ")?.first)!)

This will look for the index of the first occurrence of " ", and thereafter return the substring from start of original string up only to the first occurrence. I.e., it will never investigate or make use of the original (in this context: assumed large) string beyond they point of the first " " occurrence.

You should note, however, that due to the force unwrap (operator (!)), this will crash at runtime if the string does not contain any instance of " ". So to stay safe, and follow the optional convention of Swift, use it in an if let clause:

if let myRange = date.rangeOfString(" ") {
    let firstDateEntryFast = date.substringToIndex(myRange.first!)
        // at this point, you know myRange is non-empty, and hence, .first can be
        // force-unwrapped
}
else {
    let firstDateEntryFast = date
        // no " " separation, unexpected? -> action
}

As in my first version of the answer, split can be used as an alternative (comparable with componentsSeparatedByString):

var date = "1,340d 1h 15m 52s"
let dateAsArray = date.characters.split(" ").map{ String($0) }
let firstDateEntry = dateAsArray[0]

Alternatively, skip storing them all in an array and directly get the first entry

var date = "1,340d 1h 15m 52s"
let firstDateEntryDirectly = String(date.characters.split(" ")[0])

Swift 3 version

var str = "Hello, playground one two three"
let firstWord = str.components(separatedBy: " ").first

Tags:

Ios

Swift