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

前端 未结 22 823
感情败类
感情败类 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 20:00

    I would do it using a subscript (s[start..):

    Swift 3, 4, 5

    let s = "www.stackoverflow.com"
    let start = s.startIndex
    let end = s.index(s.endIndex, offsetBy: -4)
    let substring = s[start..

提交回复
热议问题