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

前端 未结 22 848
感情败类
感情败类 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:43

    func substr(myString: String, start: Int, clen: Int)->String
    
    {
      var index2 = string1.startIndex.advancedBy(start)
      var substring2 = string1.substringFromIndex(index2)
      var index1 = substring2.startIndex.advancedBy(clen)
      var substring1 = substring2.substringToIndex(index1)
    
      return substring1   
    }
    
    substr(string1, start: 3, clen: 5)
    

提交回复
热议问题