Calculate number of a letter in a string using Dart

void main(){
  String str = "yours string";
  Map<String, int> map = {};
  for(int i = 0; i < str.length; i++){
    int count = map[str[i]] ?? 0;
     map[str[i]] = count + 1;
  }
  print(map);
}

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.


You need to iterate on the characters and count them, you comment above contains a general function for building a histogram, but the version that just counts 'a's is probably good for you to write. I'll just show you how to loop over characters:

var myString = "hello";
for (var char in myString.splitChars()) {
  // do something
}

void main() {
  const regExp = const RegExp("a");
  print(regExp.allMatches("Hello Jordania").length); // 2
}