How to get substring with specific ranges in Swift 4?

Kent Wong picture Kent Wong · Sep 26, 2017 · Viewed 30.6k times · Source

This is using the example code from the official Swift4 doc

let greeting = "Hi there! It's nice to meet you! 👋"
let endOfSentence = greeting.index(of: "!")!
let firstSentence = greeting[...endOfSentence]
// firstSentence == "Hi there!"

But lets say let greeting = "Hello there world!" and I want to retrieve only the second word (substring) in this sentence? So I only want the word "there".

I've tried using "world!" as an argument like let endOfSentence = greeting.index(of: "world!")! but Swift 4 Playground doesn't like that. It's expecting 'Character' and my argument is a string.

So how can I get a substring of a very precise subrange? Or get nth word in a sentence for greater use in the future?

Answer

Charles Srstka picture Charles Srstka · Sep 26, 2017

You can search for substrings using range(of:).

import Foundation

let greeting = "Hello there world!"

if let endIndex = greeting.range(of: "world!")?.lowerBound {
    print(greeting[..<endIndex])
}

outputs:

Hello there 

EDIT:

If you want to separate out the words, there's a quick-and-dirty way and a good way. The quick-and-dirty way:

import Foundation

let greeting = "Hello there world!"

let words = greeting.split(separator: " ")

print(words[1])

And here's the thorough way, which will enumerate all the words in the string no matter how they're separated:

import Foundation

let greeting = "Hello there world!"

var words: [String] = []

greeting.enumerateSubstrings(in: greeting.startIndex..<greeting.endIndex, options: .byWords) { substring, _, _, _ in
    if let substring = substring {
        words.append(substring)
    }
}

print(words[1])

EDIT 2: And if you're just trying to get the 7th through the 11th character, you can do this:

import Foundation

let greeting = "Hello there world!"

let startIndex = greeting.index(greeting.startIndex, offsetBy: 6)
let endIndex = greeting.index(startIndex, offsetBy: 5)

print(greeting[startIndex..<endIndex])