獲取結(jié)尾兩個(gè)字符子串:
let sessionId = "this is a test"
let index = sessionId.index(sessionId.endIndex, offsetBy: -2)
let suffix = sessionId.substring(from: index)
最后結(jié)果為:“st”
獲取開頭字符兩個(gè):
let sessionId = "this is a test"
let index = sessionId.index(sessionId.startIndex, offsetBy: 2)
let prefix = sessionId.substring(to: index)
最后結(jié)果為:“th”