获取末尾两个字符:
let sessionId = "this is a test"
let index = sessionId.index(sessionId.endIndex, offsetBy: -2)
let suffix = sessionId.substring(from: index)
最后结果为:“st”
获取开头字符两个:
let sessionId = "this is a test"
let index = sessionId.index(sessionId.startIndex, offsetBy: 2)
let prefix = sessionId.substring(to: index)
获取中间字符两个:
a,笨方法
let sessionId = "this is a test"
let index = sessionId.index(sessionId.startIndex, offsetBy: 5)
let prefix = sessionId.substring(to: index)
let index = sessionId.index(prefix.startIndex, offsetBy: -2)
b,直接用range截取
let subString1 = (swiftStringasNSString).substring(with: NSMakeRange(1,3))