Substring in Swift 3.0
- Info:
- macOS 10.12.2 Beta
- Xcode 8.2 Beta
- Swift 3.0
前言
String.Index
Basics
索引与下标,即 index 和 subscript。一般来说,两者的概念是类似的,不过个人觉得索引有泛指的概念,而下标是具体的。
Swift 中字符串的索引类型并不是其他语言中的整型(int),而是 String.Index。String.Index,即标注字符串的索引类型。在 Swift 的标准库中,可以看到其本质是 String.CharacterView.Index
的别名。
/// The index type for subscripting a string.
public typealias Index = String.CharacterView.Index
再进一步查看 String.CharacterView
,即创建给定字符串的字符视图类型。
public struct CharacterView {
/// Creates a view of the given string.
public init(_ text: String)
}
start & end
字符串必然是一个字符的有限序列,Swift 为了方便开发者迅速定位,便集成了 startIndex
和 endIndex
。但是需要注意的是:startIndex
是指字符串的第一个字符的下标,而 endIndex
是指字符串的最后一个字符之后的下标。当字符串为空时,startIndex
和 endIndex
相同。
var str =
print(str.characters.count)
print(str.startIndex)
print(str.endIndex)
// 12
// Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)
// Index(_base: Swift.String.UnicodeScalarView.Index(_position: 12), _countUTF16: 0)
从上面的 Demo 也可以看出,startIndex
的位置为 0,而 endIndex
的位置为 12,等同于字符串的长度,而不是字符串长度减一。
除了给出了起始和结尾的下标,Swift 也提供了根据下标定位其他索引的方法:
public func index(after i: String.Index) -> String.Index
public func index(before i: String.Index) -> String.Index
public func index(_ i: String.Index, offsetBy n: String.IndexDistance) -> String.Index
public func index(_ i: String.Index, offsetBy n: String.IndexDistance, limitedBy limit: String.Index) -> String.Index?
这样就可以通过给出的 startIndex
和 endIndex
来定位到其他的下标了。
var str =
// 返回传入下标之后的下标
print(str.index(after: str.startIndex))
// 返回传入下标之前的下标
print(str.index(before: str.endIndex))
// 返回传入下标偏移后的下标(偏移量可正可负可为 0)
print(str.index(str.startIndex, offsetBy: 1))
// print(str.index(str.endIndex, offsetBy: 10))
// 作用同上,但如果超过传入的界限返回 nil
print(str.index(str.endIndex, offsetBy: 10, limitedBy: str.endIndex) ?? "越界")
下标之间的间距,也可以利用 func distance(from start: String.Index, to end: String.Index) -> String.IndexDistance
方法求出:
var str =
print(str.distance(from: str.startIndex, to: str.endIndex))
// 12
print(str.distance(from: str.endIndex, to: str.startIndex))
// -12
Range
Range 即范围,Swift 中实现了 Comparable 协议的类型都可以用 Range 来表示范围。以下的 Range 特指:Range<String.Index>
。
Range 的构造方法是 init(uncheckedBounds bounds: (lower: Bound, upper: Bound))
。即传入一个元组,返回一个范围。需要注意的是:这个范围 Swift 是不会检查的,需要程序员自觉维护。
var str =
// 前闭后开
let rangeA = Range(uncheckedBounds: (str.startIndex, str.endIndex))
print(rangeA)
// Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)..<Index(_base: Swift.String.UnicodeScalarView.Index(_position: 12), _countUTF16: 0)
print(str.substring(with: rangeA))
//
// 即使范围首尾颠倒,也没有报错
let rangeB = Range(uncheckedBounds: (str.endIndex, str.startIndex))
print(rangeB)
// Index(_base: Swift.String.UnicodeScalarView.Index(_position: 12), _countUTF16: 0)..<Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)
// 但在使用中会出错
// fatal error: Can't form Range with upperBound < lowerBound
// str.substring(with: rangeB)
在上面的 Demo 中,从输出中也可以看出,init(uncheckedBounds bounds: (lower: Bound, upper: Bound))
构造的范围是一个前闭后开的区间。
Swift 中,字符串本身也能构造出 Range,例如:得到字符串子串的范围,若不存在则返回 nil:
var str =
// 返回前闭后开的范围
print(str.range(of: ?? "不存在")
// Index(_base: Swift.String.UnicodeScalarView.Index(_position: 8), _countUTF16: 1)..<Index(_base: Swift.String.UnicodeScalarView.Index(_position: 12), _countUTF16: 0)
substring
在 Range 一节的 Demo 中,已经使用了 substring(with:)
方法测试范围的使用。Swift 中的字符串截取与其他语言其实是相似的,都是根据索引或索引范围来截取,只是 Swift 中的索引的类型不是整型,稍有麻烦。
var str =
// 截取传入范围(左开右闭)的子串
let range = Range(uncheckedBounds: (str.startIndex, str.endIndex))
print(str.substring(with: range))
// 从传入的索引开始截取到末尾(含 str.startIndex 元素)
print(str.substring(from: str.startIndex))
// 从传入的索引的前一个位置开始截取到头部(不含 str.endIndex 元素)
print(str.substring(to: str.endIndex))
//
//
//
以上的 Demo 便是 Swift 中最基本的截取字符串使用。然而有时候条件一多,代码的重复率也会增加,那么可以利用 Swift 中的 extension 来扩展原有的 String,让 Swift 的 String 可以像 C#、Java 一样截取字符串,需要注意的是 这里的 IndexDistance 实际上是 String.CharacterView.IndexDistance 的别名,而 String.CharacterView.IndexDistance 又是 Int 类型的别名。
extension String {
func substring(from: IndexDistance) -> String? {
let index = self.index(self.startIndex, offsetBy: from)
return str.substring(from: index)
}
func substring(to: IndexDistance) -> String? {
let index = self.index(self.startIndex, offsetBy: to + 1)
return str.substring(to: index)
}
func substring(with range: Range<IndexDistance>) -> String? {
let lower = self.index(self.startIndex, offsetBy: range.lowerBound)
let upper = self.index(self.startIndex, offsetBy: range.upperBound)
let range = Range(uncheckedBounds: (lower, upper))
return str.substring(with: range)
}
func substring(_ lower: IndexDistance, _ range: IndexDistance) -> String? {
let lowerIndex = self.index(self.startIndex, offsetBy: lower)
let upperIndex = self.index(lowerIndex, offsetBy: range)
let range = Range(uncheckedBounds: (lowerIndex, upperIndex))
return str.substring(with: range)
}
}
print(str.substring(to: 0) ?? "nil")
print(str.substring(from: 2) ?? "nil")
print(str.substring(with: 0..<1) ?? "nil")
print(str.substring(1, 2) ?? "nil")
// m
//
// m
// ai
参考资料
Documentation & API Reference