Swift: How to get substring from start to last index of character

前端 未结 22 852
感情败类
感情败类 2020-11-30 19:09

I want to learn the best/simplest way to turn a string into another string but with only a subset, starting at the beginning and going to the last index of a character.

22条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 19:40

    edit/update:

    In Swift 4 or later (Xcode 10.0+) you can use the new BidirectionalCollection method lastIndex(of:)

    func lastIndex(of element: Element) -> Int?
    

    let string = "www.stackoverflow.com"
    if let lastIndex = string.lastIndex(of: ".") {
        let subString = string[..

提交回复
热议问题