swift字符串截取相关操作
2019-11-06 本文已影响0人
SAW_
快2个月没写swift代码,昨天刚想写个字符串截取的东西,习惯性的带上OC的操作,然后懵逼了,想想还是写点东西记录巩固下。有关swift字符串的语法就不打出来,请参考这里字符串和字符
OC中对于字符串NSString的截取所提供的api简单明了,如下
//截取掉下标7之前的字符串
NSString *string1 = @"123456789";
string1 = [string1 substringToIndex:7];//(length为7)
NSLog(@"截取的值为:%@",string1);//输出结果为1234567
//截取掉下标2之后的字符串
NSString *string2 = @"123456789";
string2 = [string2 substringFromIndex:2];
NSLog(@"截取的值为:%@",string2);//输出结果为3456789
//截取字符串最后3位
NSString *string3 = @"123456789";
string3 = [string3 substringFromIndex:string3.length- 3];
NSLog(@"截取的值为:%@",string3);//输出结果为789
//即当截取字符串后n位时,公式为:
//string = [sting substringfromIndex:string.length-n];
现在在swift中就不支持这样操作了,String的下标方法,根本不支持Int类型的下标
subscript(bounds: Range<String.Index>) -> String { get }
Accesses the text in the given range.
subscript(i: String.Index) -> Character { get }
Accesses the character at the given position.
subscript(bounds: ClosedRange<String.Index>) -> String { get }
Accesses the text in the given range.
Swift标准库只支持的三种下标访问String字符串的方法
Range<String.Index>:元素为String.Index类型的Range(开区间)
String.Index:String.Index元素
ClosedRange<String.Index>:元素为String.Index类型的CloseRange(闭区间)
所以swift中字符串的截取操作只能是如下例子
let str = "Hello World"
let oneChar = str[str.startIndex] "H"
let twoChar = str[str.index(after: str.startIndex)] "e"
let zeroChar = str[str.index(before: str.endIndex)] "d"
let threeChar = str[str.index(str.startIndex, offsetBy: 1)] "e"
let fourChar = str[str.index(str.startIndex, offsetBy: 2)] "l"
let fiveChar = str[str.index(str.startIndex, offsetBy: 0)] "H"
/**
str.endIndex,这个index索引并不是最后一个d字母,而是字符串的“结束后”位置——即比最后一个有效下标参数大1的位置。
所有直接str[str.endIndex]是会导致越界崩溃的
*/
//let sixChar = str[str.endIndex]
let sevenChar = str[str.index(str.endIndex, offsetBy: -1)] "d"
let eightChar = str[str.index(str.endIndex, offsetBy: -2)] "l"
let nineChar = str[str.startIndex..<str.endIndex] "Hello World"
let tenChar = str[str.startIndex...str.index(before: str.endIndex)] "Hello World"
/**
let temp = str[0]
let temp = str[1]
let temp = str[2]
let temp = str[3]
*/
//等价于下面写法
let temp0 = str[str.index(str.startIndex, offsetBy: 0)] "H"
let temp00 = str[str.startIndex] "H"
let temp1 = str[str.index(str.startIndex, offsetBy: 1)] "e"
let temp2 = str[str.index(str.startIndex, offsetBy: 2)] "l"
let temp3 = str[str.index(str.startIndex, offsetBy: 3)] "l"
我就纳闷了截取给字符要写这么多代码,这不符合程序员偷懒的特性,还好我们可以通过extension来扩展String的截取方法,分别通过重新定义下标subscript跟自定义func来解决
extension String {
/// 截取到任意位置
func subString(to: Int) -> String {
let index: String.Index = self.index(startIndex, offsetBy: to)
return String(self[..<index])
}
/// 从任意位置开始截取
func subString(from: Int) -> String {
let index: String.Index = self.index(startIndex, offsetBy: from)
return String(self[index ..< endIndex])
}
/// 从任意位置开始截取到任意位置
func subString(from: Int, to: Int) -> String {
let beginIndex = self.index(self.startIndex, offsetBy: from)
let endIndex = self.index(self.startIndex, offsetBy: to)
return String(self[beginIndex...endIndex])
}
//使用下标截取到任意位置
subscript(to: Int) -> String {
let index = self.index(self.startIndex, offsetBy: to)
return String(self[..<index])
}
//使用下标从任意位置开始截取到任意位置
subscript(from: Int, to: Int) -> String {
let beginIndex = self.index(self.startIndex, offsetBy: from)
let endIndex = self.index(self.startIndex, offsetBy: to)
return String(self[beginIndex...endIndex])
}
}
所以通过自定义extension的方法来操作截取如下
//所以写法用自定义方法截取
let func1 = str.subString(to: 2) "He"
let func2 = str.subString(from: 4) "o World"
let func3 = str.subString(from: 2, to: 5) "llo"
//所以写法用下标方式获取
let subscript1 = str[0] ""
let subscript2 = str[1] "H"
let subscript3 = str[2, 5] "llo"