String总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// 获取String字符串长度
str.count

/// 截取第一个关键字之前的内容
let name = "Marie Curie"
let firstSpace = name.firstIndex(of: " ") ?? name.endIndex
let firstName = name[..<firstSpace]

/// 拆分字符串为数组
Array(name)

/// 获取前几位字符
let string = "Helloworld"
string.dropLast(string.count - 6)

/// 获取后几位字符
let string = "Helloworld"
string.dropFirst(string.count - 6)

/// 获取最后一位字符
var string = "Hellowaeorld"
string.popLast()