String, substring, Range, NSRange in Swift 4

前端 未结 2 366
情话喂你
情话喂你 2021-01-07 23:54

I am using the following code to get a String substring from an NSRange:

func substring(with nsrange: NSRange) -> String? {
             


        
2条回答
  •  北海茫月
    2021-01-08 00:18

    With Swift 4 we can get substrings this way.

    1. Substring from index

      let originStr = "Test"
      let offset = 1
      let str = String(originStr.suffix(from: String.Index.init(encodedOffset: offset)))
      
    2. Substring to index

      let originStr = "Test"
      let offset = 1
      String(self.prefix(index))
      

提交回复
热议问题