Mango Snippets » #1

Swift: How To Count Occurrences of a Character in a Swift String

To count occurrences of a character in a Swift String, you can use Sequence.filter and then Array.count:

"Mango Snippets".filter({ $0 == "n"}).count

Output:

2