在Swift字符串的檢索中,有時(shí)候返回的可能是一個(gè)范圍,而不是一個(gè)具體的位置.下面看一下String.Index和 Range
String.Index:表示在字符的CharacterView實(shí)例的位置,進(jìn)入頭文件就可以看到,其實(shí)是public typealias Index = String.CharacterView.Index。
Range:一個(gè)半開半閉的可比較范圍,從下界(lower bound)到上界(upper bound),但是不包括上界。我們可以使用半開半閉操作符(..<)來創(chuàng)建Range實(shí)例。而且我們可以使用該實(shí)例來迅速確認(rèn)一個(gè)值是否在該范圍之內(nèi),如:
let underFive =0.0..<5.0
print(underFive.contains(3.14)) // Prints "true"
print(underFive.contains(6.28)) // Prints "false"
print(underFive.contains(5.0)) // Prints "false"
注意:Range實(shí)例能夠代表一個(gè)空的范圍,如下:
let empty =0.0..<0.0
print(empty.contains(0.0)) // Prints "false"
print(empty.isEmpty) // Prints "true"
具體使用
var str = "Hello Merry Christmas day!"
1:搜索字符串
let range = str.range(of:"Hello") //正向檢索
let backWardsRange = str.range(of:"Hello", options: .backwards)//反向檢索
let caseInsensitiveRange = str.range(of:"day", options: .caseInsensitive, range:nil , locale:nil)//忽略大小寫
2:從字符串指定范圍查找特定字符串
//startIndex 第一個(gè)字符的位置
let startIndex: String.Index =str.startIndex
str[startIndex]//H
//endIndex 最后一個(gè)字符的位置
let endIndex: String.Index =str.endIndex
//str[endIndex] // error: after last character,fatal error: Can't form a Character from an empty String
//after 給定位置之后的字符位置
let afterIndex: String.Index =str.index(after:startIndex)
str[afterIndex]//e
//range 指定范圍
let afterRange = str.index(after:startIndex)..<str.endIndex
str[afterRange]//ello Merry Christmas day!
//before 給定位置之前的字符位置
let beforeIndex: String.Index =str.index(before:str.endIndex)
str[beforeIndex]//!
//offsetBy 偏移量,可以是正數(shù)或者負(fù)數(shù)
//這里是以字符串開始位置為標(biāo)準(zhǔn),向后偏移21個(gè)位置
let offsetIndex: String.Index =str.index(str.startIndex, offsetBy:12)
str[offsetIndex]//C
let offsetRange = startIndex ..< offsetIndex
str[offsetRange]//Hello Merry
//limitedBy 給定限制范圍,確保不會(huì)越界
if let index =str.index(str.startIndex, offsetBy:10, limitedBy: str.endIndex){
str[index]//y
}
let subStringRange = str.range(of:"Merry", options: .caseInsensitive, range:offsetRange)
print(subStringRange)
//distance 查找字符的位置
let string = "Hello.World"
let needle: Character ="."
if let idx =string.characters.index(of:needle) {
let pos =string.characters.distance(from:string.startIndex, to: idx)
print("Found\(needle) at position\(pos)")//Found . at position 5
}
else {
print("Not found")
}
3:截取子串
let subIndex: String.Index =str.index(str.startIndex, offsetBy:12)
str.substring(to:endIndex) //從開始位置到endIndex范圍內(nèi)的字符
str.substring(from:subIndex) //從subIndex到最后范圍內(nèi)的字符
str.substring(with:subIndex..<endIndex)//截取某一個(gè)范圍內(nèi)的字符
str //注意:對字符串進(jìn)行截取操作,原來的字符串不變 Hello Merry Christmas day!
4:插入字符到字符串
str.insert("L", at:startIndex)//LHello Merry Christmas day!
5:末尾追加字符串
str.append("A Apple day!")//LHello Merry Christmas day!A Apple day!
6:使用字符串替換指定范圍的子字符串
str.replaceSubrange(str.startIndex..<str.index(str.startIndex, offsetBy: 6), with:"Hello") //"Hello Merry Christmas day!A Apple day!
7:去除字符
str.remove(at:startIndex) //去除指定位置的字符
str.removeSubrange(subIndex..<endIndex)//去除指定范圍的字符
str.removeAll()//去除所有 str