Swift Language Strings and Characters Count occurrences of a Character into a String


Given a String and a Character

let text = "Hello World"
let char: Character = "o"

We can count the number of times the Character appears into the String using

let sensitiveCount = text.characters.filter { $0 == char }.count // case-sensitive
let insensitiveCount = text.lowercaseString.characters.filter { $0 == Character(String(char).lowercaseString) } // case-insensitive