How to create range in Swift?

前端 未结 10 1497
时光说笑
时光说笑 2020-11-28 01:37

In Objective-c we create range by using NSRange

NSRange range;

So how to create range in Swift?

10条回答
  •  無奈伤痛
    2020-11-28 02:25

    Use like this

    var start = str.startIndex // Start at the string's start index
    var end = advance(str.startIndex, 5) // Take start index and advance 5 characters forward
    var range: Range = Range(start: start,end: end)
    
    let firstFiveDigit =  str.substringWithRange(range)
    
    print(firstFiveDigit)
    

    Output : Hello

提交回复
热议问题