How can I count the number of a letter in a string using Dart?
contains, counting, dart, string
Solution
Here's a simple way to do it:
void main() {
print('a'.allMatches('Hello Jordania').length); // 2
}
Edit: the tested string is the parameter, not the character to be counted.
Problem
I'm trying to calculate how many of the letter `a` I have in the sentence: `Hello Jordania`. I found the function `contains()`. I'm using it like this: ``` var phrase = "Hello Jordania"; var comptenbrdea = phrase.contains('a'); print(comptenbrdea); ``` As we know, I got `True` as a response. I couldn't find the right function to calculate how many times I get an `a`. I might be able to do something with a loop if I can check every single character? I'm lost on this.