Swift3.0 中不能直接使用下标数字进行字符串截取,只能使用String.Index来做位置索引,要想实现截取功能首先得获取到String.Index;
let mainString = "0123456789" let index = mainString.index(mainString.endIndex,offsetBy: -1) print(mainString.substring(from: index)) print(mainString.substring(to: index))
打印结果:9
打印结果:012345678
let mainString = "0123456789" let index = mainString.index(mainString.startIndex,offsetBy: 1) print(mainString.substring(from: index)) print(mainString.substring(to: index))
打印结果:123456789
打印结果:0
原文链接:https://www.f2er.com/swift/322598.html