swift3.0 中不能直接使用下标数字进行字符串解决,只能使用String.Index来做位置索引,要想实现截取功能首先得获取到String.Index;
下面两段代码获取开头可结尾,获取中间部分参数用Range<Index>即可;
1获取结尾两个字符子串:
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)
最后结果为:“th”
二、String类中的API变化
除了Swift版的Cocoa框架中的API有了大范围的修改外,Swift的一些核心库也有了很大的改动。
Swift3.0中的字符串类型String在方法API上更加简洁,其中变动较大的是与下标相关的方法,列举如下:
var string = "Hello-Swift"
//获取某个下标后一个下标对应的字符 char="e"
//swift2.2
//var char = string[startIndex.successor()]
//swift3.0
var char = string[string.index(after: startIndex)]
//获取某个下标前一个下标对应的字符 char2 = "t"
//swift2.2
//var char2 = string[endIndex.predecessor()]
//swift3.0
var char2 = string[string.index(before: string.endIndex)]
//通过范围获取字符串中的一个子串 Hello
//swift2.2
//var subString = string[startIndex...startIndex.advancedBy(4)]
//swift3.0
var subString = string[startIndex...string.index(startIndex, offsetBy: 4)]
//swift2.2
//var subString2 = string[endIndex.advancedBy(-5)...endIndex.predecessor()]
//swift3.0
var subString2 = string[string.index(endIndex, offsetBy: -5)..<endIndex]
//获取某个子串在父串中的范围
//swift2.2
//var range = string.rangeOfString("Hello")
//swift3.0
var range = string.range(of: "Hello")
//追加字符串操作 此时string = "Hello-Swift! Hello-World"
//swift2.2
//string.appendContentsOf(" Hello-World")
//swift3.0
string.append(" Hello-World")
//在指定位置插入一个字符 此时string = "Hello-Swift!~ Hello-World"
//swift2.2
//string.insert("~", atIndex: string.startIndex.advancedBy(12))
//swift3.0
string.insert("~", at: string.index(string.startIndex, offsetBy: 12))
//在指定位置插入一组字符 此时string = "Hello-Swift!~~~~ Hello-World"
//swift2.2
//string.insertContentsOf(["~","~","~"], at: string.startIndex.advancedBy(12))
//swift3.0
string.insert(contentsOf: ["~","~","~"], at: string.index(string.startIndex, offsetBy: 12))
//在指定范围替换一个字符串 此时string = "Hi-Swift!~~~~ Hello-World"
//swift2.2
//string.replaceRange(string.startIndex...string.startIndex.advancedBy(4), with: "Hi")
//swift3.0
string.replaceSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 4), with: "Hi")
//在指定位置删除一个字符 此时string = "Hi-Swift!~~~~ Hello-Worl"
//swift2.2
//string.removeAtIndex(string.endIndex.predecessor())
//swift3.0
string.remove(at: string.index(before:string.endIndex))
//删除指定范围的字符 此时string = "Swift!~~~~ Hello-Worl"
//swift2.2
//string.removeRange(string.startIndex...string.startIndex.advancedBy(2))
//swift3.0
string.removeSubrange(string.startIndex...string.index(string.startIndex, offsetBy: 2))
var string2 = "My name is Jaki"
//全部转换为大写
//swift2.2
//string2 = string2.uppercaseString
//swift3.0
string2 = string2.uppercased()
//全部转换为小写
//swift2.2
//string2 = string2.lowercaseString
//swift3.0
string2 = string2.lowercased()
需要注意,在Swift3.0中Range结构体被划分成了两种类型,Range和ClosedRange,分别用来描述左闭右开区间和闭区间,对应到运算符为0..<10和0...10。
从上面的示例代码中可以看出,String类型中的很多方法命名进行了Swift风格的简化,改动较大的一个点是关于下标index的改变,移除了两个Index下标移动的方法,使用String类型的index()方法来进行下标的移动操作,编程更加安全。