Search code examples
swiftstringcountcharacter

Count certain character in a String


my question is if it is possible in Swift to count how many characters are located in a string.

For example I have the following string:

"100,101,305,222"

Now I want to know how many commas are in this string? Is it possible to find out in Swift?


Solution

    1. Filter by the character you want
    2. Count them
    "100,101,305,222"    
        .filter { $0 == "," }
        .count
    

    It is . Nothing to do with the