How can I count the occurrence of a character in a string?
Example
String: 123-456-7890
I want to find the occurrence count of "-" in given string
How can I count the occurrence of a character in a string?
Example
String: 123-456-7890
I want to find the occurrence count of "-" in given string
I did this for you. try this.
The current selected answer will fail if the string starts or ends with the character you are checking for.
Use this instead:
This will do the work,
You can simply do it like this:
Output:
Counted times: 2
this worked. hope it helps. happy coding :)
The
replaceOccurrencesOfString:withString:options:range:
method returns the number of replacements that were made, so we can use that to work out how many-
s are in your string.