字符串
基本屬性
- String 是一個結(jié)構(gòu)體,性能更高
- NSString 是一個 OC 對象,性能略差
- String 支持直接遍歷
- Swift 提供了 String 和 NSString 之間的無縫轉(zhuǎn)換
- let NStr = str as NSString
空字符串:String()
- 字節(jié)長度:let byteNum = str.lengthOfBytes(using: .utf8)
- 字符個數(shù):let length = str.count
let str = "abc" + "一二三" + "??"
print(str.lengthOfBytes(using: .utf8))
print(str.count)
// Prints "16 7"
合并
- 純字符串"+"
- 不是 String 類型時:
A.顯示轉(zhuǎn)換 String(s)
B.字符串插值\()
let str1 = 20
let str2 = "a" + String(str1)
let str3 = "a" + "\(str1)"
format
let h = 9, m = 8, s = 20
//參數(shù)可以多寫褒傅,只是不接收,不會報錯
print(String(format: "%02d:%02d:%02d",h,m,s))
// Prints: "09:08:20"
substring
方法一:as NSString
let s3 = (s as NSString).substring(from: 2)
方法二:由 index 取 string[aIndex..<bIndex] 或 prefix/suffix
總結(jié):
/// substring from index
/// - Parameters:
/// - from: from: from
/// - include: default is true
/// - Returns: string
func utils_substring(from: Int, include: Bool = true) -> String {
if from >= count {
return ""
}
let start = index(startIndex, offsetBy: from)
let sub = include ? self[start...] : self[start...].dropFirst()
// let sub = suffix(from: start)
return String(sub)
}
/// substring to index
/// - Parameters:
/// - to: to
/// - include: default is false
/// - Returns: string
func utils_substring(to: Int, include: Bool = false) -> String {
if to >= count {
return self
}
let end = index(startIndex, offsetBy: to)
let sub = include ? self[...end] : self[..<end]
// let sub = prefix(upTo: end) // 開區(qū)間
return String(sub)
}
/// substring from left index to right index
/// - Parameters:
/// - left: left index
/// - right: right index
/// - Returns: string
func utils_substring(left: Int = 0, right: Int = 0) -> String {
if left >= count || right >= count || left + right >= count {
return ""
}
let start = index(startIndex, offsetBy: left)
let end = index(endIndex, offsetBy: -right)
let sub = self[start..<end]
// let sub = suffix(from: start).prefix(upTo: end)
return String(sub)
}
/// 匹配兩邊字符袄友,取中間內(nèi)容 (開區(qū)間)
/// - Parameters:
/// - left: left
/// - right: right
/// - Returns: string
func utils_substring(left: String = "", right: String = "") -> String {
let start = left == "" ? startIndex : (firstIndex(of: Character(left)) ?? startIndex)
let end = right == "" ? endIndex : (lastIndex(of: Character(right)) ?? endIndex)
var sub = self[start..<end]
// var sub = suffix(from: start).prefix(upTo: end)
if start != startIndex {
sub = sub.dropFirst()
}
else if start == startIndex, left != "" {
if contains(Character(left)) {
sub = sub.dropFirst()
}
}
return String(sub)
}
字符串遍歷
String 支持直接遍歷
for n in "abc" {
print(n, terminator:" ")
}
// Print "a b c"
數(shù)組
數(shù)組內(nèi)元素類型不一致需要手動標明 [Any]殿托,不可省略
不推薦在數(shù)組中放入不同類型的元素。
空數(shù)組:var emptyArray = [String]()
初始化:var array = ["a", "b", "c”]
增操作:array.append("e”)
刪操作:array.remove(at: 1)
改操作: array[1] = "11"
數(shù)組遍歷
for in
- 詳細信息:enumerated()(字典不需要加此方法)
- 倒序:reversed()
for a in array {
print(a)
}
for (offset, element) in array.reversed().enumerated() {
print(offset, element)
}
數(shù)組合并
array1 + array2
let array1 = ["a", "b", "c"]
// 數(shù)組內(nèi)元素類型不一致需要手動標明[Any]剧蚣,不可省略
let array2: [Any] = ["a", "0", 20]
let array3 = array1 + array2
字典
value 類型不一致時需要手動標明字典類型 [String: Any]
空字典:var emptydict = [String: Any]()
初始化:var dict: [String: Any] = ["name": "小王", "age": 18]
增操作:dict["title"] = "增加"
刪操作:dict.removeValue(forKey: "title")
改操作:dict["title"] = "修改"
字典遍歷
for in
順序是key, value支竹,和命名無關(guān)
for (key, value) in dict {
print(key, value)
}
字典合并
遍歷元素少的字典,添加到另一個中
var dict: [String: Any] = ["name": "zz", "age": 20]
let dict1: [String: Any] = ["title1": "aaaaa1", "title2": "aaaaa2"]
for (key, value) in dict1 {
dict[key] = value
}