文章目录
向上向下取整
print(ceil(1.5)) // 向上取整 2
print(floor(1.5)) // 向下取整 1
四舍五入
print(lround(1.4)) // 1
print(lround(1.5)) // 2
直接截断小数部分
- 使用强制转换会将浮点部分去除
print(Int(1.4)) // 1
print(Int(1.5)) // 1
保留指定小数位
- 保留两位小数
var f = 123.123
var s = String(format:"%.2f", f)
print(s) // 123.12
前置位补0
- 不足两位时前面补0
var f = 1
var s = String(format:"%.2d", f)
print(s) // 01
相除
print(3 / 2) // 1 不是 2
print(ceil(3 / 2)) // 2.0
取余数 取模
print(3 % 2) // 1
// 浮点数取余
print(3.5.truncatingRemainder(dividingBy: 2)) // 1.5
最大值 最小值
print(max(1, 3, 2)) // 3
print(min(1, 3.3)) // 1.0
绝对值
print(abs(-12)) // 12
print(abs(13.1)) // 13.1
开方
let a = sqrt(9)
print(a) // 3.0
π
print(Float.pi) // 3.1415925
print(Double.pi) // 3.141592653589793
ASCII
ASCII码一共128个
常见ASCII码的大小规则:0-9<A-Z<a-z
几个常见字母的ASCII码大小: “A”为65;“a”为97;“0”为 48
- 判断字符是否是 ASCII
let char1: Character = "a"
print(char1.isASCII) // true
let char2: Character = "🐶"
print(char2.isASCII) // false
- 获取字符的 ASCII 码值
let char1: Character = "a"
print(char1.asciiValue ?? "木有") // 97
let char2: Character = "🐶"
print(char2.asciiValue ?? "木有") // 木有
- 通过 ASCII 码值转字符
let asciiValue = 97
let char: Character = Character(UnicodeScalar(asciiValue)!)
print(char) // a
字符串遍历
let str: String = "abc"
str.forEach { char in
print(char) // a b c
}
字符串分割
- 指定分割字符
let str = "a b c"
let array = str.split(separator: " ")
print(array) // ["a", "b", "c"]
- 指定分割字符集
let string = "1@2#3@4#5"
let array2 = string.components(separatedBy: CharacterSet.init(["@", "#"]))
print(array2) // ["1", "2", "3", "4", "5"]
- 指定分割条件
let str = "abcde"
let array = str.split { char in
return char == "b" || char == "d"
}
print(array) // ["a", "c", "e"]
- 指定最大分割次数
let string = "1@2#3@4#5"
// maxSplits表示最大分割次数
let array4 = string.split(separator: "@", maxSplits: 1, omittingEmptySubsequences: false)
print(array4) // ["1", "2#3@4#5"]
- 是否忽略空字符串
// omittingEmptySubsequences表示忽略空字符串
let string = "1@2#3@4#5"
let array5 = "1#2#".split(separator: "#", maxSplits: Int.max, omittingEmptySubsequences: false)
print(array5) // ["1", "2", ""]
let array6 = "1#2#".split(separator: "#", maxSplits: Int.max, omittingEmptySubsequences: true)
print(array6) // ["1", "2"]
字符串转字符数组
let str = "abc"
let array = str.map { $0 }
print(array) // ["a", "b", "c"]
字符串截取
- 截取前几位
let str: String = "abcd"
let prefix = str.prefix(2)
print(prefix) // ab
- 截取后几位
let str: String = "abcd"
let suffix = str.suffix(2)
print(suffix) // cd
- 去掉前几位
let str = "abcd"
let dropStr = str.dropFirst(2)
print(dropStr) // cd
- 去掉后几位
let str = "abcd"
let dropStr = str.dropLast(2)
print(dropStr) // ab
- 截取指定范围
// NSString 和 NSRange 才是王道
let str1: NSString = "abcde"
let range: NSRange = NSRange(location: 1, length: 2)
let subString = str1.substring(with: range)
print(subString) // bc
- 不用 range 截取
let str = "abcdef"
let dropStr = str.dropFirst(2).prefix(2)
print(dropStr) // cd
字符串包含
var str = "abcdef"
if str.contains("abc") {
print("contain")
} else {
print("not contain")
}
字符串查找
- 查找子字符串的范围
// 用 NSString 和 NSRange 会简单很多
let str1: NSString = "abcde"
let str2: NSString = "bcd"
let range = str1.range(of: String(str2))
print(range) // {1, 3}
数组遍历
var array = [1, 2, 3]
array.forEach { item in
print(item) // 1 2 3
}
- 同时获取元素和下标:
let array = ["a", "b", "c"]
for (index, item) in array.enumerated() {
print("第\(index)个元素是\(item)") // 第0个元素是a ..
}
数组截取
- 使用区间运算符
var array = [1, 2, 3]
let array2 = array[0..<2]
print(array2) // [1, 2]
- 单侧区间
let array = [1, 2, 3, 4, 5]
print(array[1...]) // [2, 3, 4, 5]
print(array[...2]) // [1, 2, 3]
print(array[..<2]) // [1, 2]
- 获取前几个元素
let array = [1, 2, 3, 4, 5]
print(array.prefix(3)) // [1, 2, 3]
- 获取后几个元素
let array = [1, 2, 3, 4, 5]
print(array.suffix(3)) // [3, 4, 5]
数组替换
// NSRange 才是王道
var array = [1, 2, 3, 4, 5]
var range = Range(NSRange.init(location: 1, length: 2))
array.replaceSubrange(range!, with: [6])
print(array) // [1, 6, 4, 5]
- 使用区间运算符
var array = [1, 2, 3, 4, 5]
array[1...3] = [666]
print(array) // [1, 666, 5]
数组反转
- 直接反转原数组
var array = [1, 2, 3]
array.reverse()
print(array) // [3, 2, 1]
- 生成新数组
let array = [1, 2, 3]
let array2 = array.reversed()
print(array2) // [3, 2, 1]
数组排序
var array = [2, 1, 3]
// 默认从小到大排序
array.sort()
print(array) // 1 2 3
var array = [2, 1, 3]
// 从大到小排序
array.sort(by: >)
print(array) // 3 2 1
var array = [2, 1, 3]
// 按指定条件排序
let sortedArray = array.sorted { a, b in
return a > b
}
print(sortedArray) // 3 2 1
数组映射
let array = [1, 2, 3]
let array2 = array.map { item in
item * item
}
print(array2) // [1, 4, 9]
- 使用
compactMap
过滤空值
let possibleNumbers = ["1", "2", "three", "///4///", "5"]
let mapped: [Int?] = possibleNumbers.map { str in Int(str) }
print(mapped) // [1, 2, nil, nil, 5]
let compactMapped: [Int] = possibleNumbers.compactMap { str in Int(str) }
print(compactMapped) // [1, 2, 5]
数组过滤
let array = [1, 2, 3, 4, 5]
let array2 = array.filter { item in
item > 3
}
print(array2) // [4, 5]
数组包含
let array = ["a", "b", "c", "a"]
if array.contains("a") {
print("包含")
}
数组查询
- 查询元素在数组中第一次出现的index
let array = ["a", "b", "c", "a"]
if let a = array.firstIndex(of: "a") {
print("a第一次出现的index是\(a)") // 0
} else {
print("无此元素")
}
- 查询元素在数组中最后一次出现的index
let array = ["a", "b", "c", "a"]
if let a = array.lastIndex(of: "a") {
print("a最后一次出现的index是\(a)") // 3
} else {
print("无此元素")
}
- 查找数组中的最大元素最小元素
let array = ["a", "b", "c", "a"]
print(array.max() ?? "无最大元素") // c
print(array.min() ?? "无最小元素") // a
- 是否所有元素都满足指定条件
let array = [1, 2, 3]
if array.allSatisfy({ item in
item > 0
}) {
print("所有元素都大于0")
}
字典遍历
let dict: [String: Int] = ["age": 20, "weight": 90]
dict.forEach { item in
print(item)
}
字典转数组
let keys = dict.keys
let values = dict.values
print(keys) // ["age", "weight"]
print(values) // [20, 90]