How to get substring with specific ranges in Swift 4?

Viewed 38118

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?

6 Answers

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])

For swift4,

let string = "substring test"
let start = String.Index(encodedOffset: 0)
let end = String.Index(encodedOffset: 10)
let substring = String(string[start..<end])

In Swift 5 encodedOffset (swift 4 func) is deprecated.
You will need to use utf160Offset

// Swift 5     

let string = "Hi there! It's nice to meet you!"
let startIndex = 10 // random for this example
let endIndex = string.count

let start = String.Index(utf16Offset: startIndex, in: string)
let end = String.Index(utf16Offset: endIndex, in: string)

let substring = String(string[start..<end])

prints -> It's nice to meet you!

There is one mistake in the first answer.

Range<String.Index>.upperBound

The upperBound property should be the endIndex For Example:

let text = "From Here Hello World"
if let result = text.range(of: "Hello World") {
     let startIndex = result.upperBound
     let endIndex = result.lowerBound
     print(String(text[startIndex..<endIndex])) //"Hello World"
}

the simplest way I use is :

var str = "abcdefg"
String(Array(str)[2...4])

Old habits die hard. I did it the "Java" way and split the string up by spaces, then accessed the second word.

print(greeting.split(separator: " ")[1]) // "there /n"
Related