将字符的出现次数计入字符串
鉴于 String
和 Character
let text = "Hello World"
let char: Character = "o"
我们可以计算 Character
出现在 String
中的次数
let sensitiveCount = text.characters.filter { $0 == char }.count // case-sensitive
let insensitiveCount = text.lowercaseString.characters.filter { $0 == Character(String(char).lowercaseString) } // case-insensitive