27
votes

i've just converted my little app but i've found this error: 'substring(from:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator

my code is:

    let dateObj = dateFormatterFrom.date(from: dateStringa)


    if dateObj != nil {
        cell.detailTextLabel?.text = dateFormatterTo.string(from:(dateObj!))
    } else {
        let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
        cell.detailTextLabel?.text = thisRecord.pubDate.substring(from: index)
    }
7
This is not a duplicate of that question. That question is about “partial range upto”, and this question is about “partial range from”. They are not the same, they are constructed with different operators, and that question doesn't have an answer for either constructing a “partial range upto” or otherwise taking the suffix of a string. - rob mayoff

7 Answers

43
votes

Follow the below example to fix this warning: Supporting examples for Swift 3, 4 and 5.

let testStr = “Test Teja”

let finalStr = testStr.substring(to: index) // Swift 3
let finalStr = String(testStr[..<index]) // Swift 4

let finalStr = testStr.substring(from: index) // Swift 3
let finalStr = String(testStr[index...]) // Swift 4 

//Swift 3
let finalStr = testStr.substring(from: index(startIndex, offsetBy: 3)) 

//Swift 4 and 5
let reqIndex = testStr.index(testStr.startIndex, offsetBy: 3)
let finalStr = String(testStr[..<reqIndex])

//**Swift 5.1.3 - usage of index**

let myStr = "Test Teja == iOS"

let startBound1 = String.Index(utf16Offset: 13, in: myStr)
let finalStr1 = String(myStr[startBound1...])// "iOS"

let startBound2 = String.Index(utf16Offset: 5, in: myStr)
let finalStr2 = String(myStr[startBound2..<myStr.endIndex]) //"Teja == iOS"
14
votes

In place of substring use suffix. Use like below :

cell.detailTextLabel?.text = String(thisRecord.pubDate.suffix(from: index))
12
votes

It means you should use the new partial range operator as your upperBound:

let str =  "Hello World !!!"
if let index = str.range(of: "Hello ")?.upperBound {
   let string = String(str[index...])  // "World !!!"
}

In your case

cell.detailTextLabel?.text = String(thisRecord.pubDate[index...]))
5
votes

In Swift 5, it is:

extension String {

    func index(from: Int) -> Index {
        return self.index(startIndex, offsetBy: from)
    }

    func substring(from: Int) -> String {
        let fromIndex = index(from: from)
        return String(self[fromIndex...])
    }

    func substring(to: Int) -> String {
        let toIndex = index(from: to)
        return String(self[..<toIndex])
    }

    func substring(with r: Range<Int>) -> String {
        let startIndex = index(from: r.lowerBound)
        let endIndex = index(from: r.upperBound)
        return String(self[startIndex..<endIndex])
    }
}
4
votes

Most of my strings have A-Za-z and 0-9 content. No need for difficult Index handling. This extension of String is based on the familiar LEFT / MID and RIGHT functions.

extension String {

    // LEFT
    // Returns the specified number of chars from the left of the string
    // let str = "Hello"
    // print(str.left(3))         // Hel
    func left(_ to: Int) -> String {
        return "\(self[..<self.index(startIndex, offsetBy: to)])"
    }

    // RIGHT
    // Returns the specified number of chars from the right of the string
    // let str = "Hello"
    // print(str.left(3))         // llo
    func right(_ from: Int) -> String {
        return "\(self[self.index(startIndex, offsetBy: self.length-from)...])"
    }

    // MID
    // Returns the specified number of chars from the startpoint of the string
    // let str = "Hello"
    // print(str.left(2,amount: 2))         // ll
    func mid(_ from: Int, amount: Int) -> String {
        let x = "\(self[self.index(startIndex, offsetBy: from)...])"
        return x.left(amount)
    }
}
1
votes
str[..<index]
str[index...]

The code above is "partial range from" Look at this How can I use String slicing subscripts in Swift 4?

0
votes

If you wish to get substring with specific offset without upper bound do the following:

let index = thisRecord.pubDate.index(thisRecord.pubDate.startIndex, offsetBy: 5)
cell.detailTextLabel?.text = String(thisRecord.pubDate[index...]

This way you create a new String object from your existing String thisRecord.pubDate taking anything from specified index to the end index of original String.