(Swift) Function to parse string (from-to)

前端 未结 1 465
半阙折子戏
半阙折子戏 2021-01-28 21:56

Is any other ways to parse string \"from-to\"? I mean when there is a value we need and we know about string before and after it. I`ve just created such functi

相关标签:
1条回答
  • 2021-01-28 22:27

    You could do something like this using substringWithRange:

    func substringBetween(start: String, and end: String) -> String? {
        if let startIndex = rangeOfString(start, options: .LiteralSearch, range: nil, locale: nil)?.endIndex,
               endIndex = rangeOfString(end, options: .LiteralSearch, range: startIndex..<self.endIndex, locale: nil)?.startIndex {
            return substringWithRange(startIndex..<endIndex)
        } else {
            return nil
        }
    }
    
    0 讨论(0)
提交回复
热议问题