How can I count the number of a letter in a string using Dart?
Asked Answered
B

6

30

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.

Bargain answered 28/10, 2012 at 16:19 Comment(2)
nuclear option: use regex to remove everything that ISN'T an a, then get the length of whatever remains.Enroot
I find thise function : Map<String, int> letterFrequency(String text) { String textWoutSpaces = text.replaceAll('\n', ''). replaceAll(' ', '').replaceAll('.', ''); List charList = textWoutSpaces.splitChars(); charList.sort((m,n) => m.compareTo(n)); var charMap = {}; for (var char in charList) { if(char == 'o'){ charMap[char] = charMap.putIfAbsent(char, () => 0) + 1; } } return charMap; }Bargain
R
83

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.

Roark answered 28/10, 2012 at 21:59 Comment(2)
I couldn't figure out why this wasn't working, then I realized that I had switched the equivalent of 'a' and 'Hello Jordania' for my use case. The correct order of putting the pattern first and the thing to search inside allMatches seems backwards to me.Lorinalorinda
Got bitten by this as well. Thanks for your comment @LukeHutchison which helped me to solve this immediately.Complementary
B
6
void main() {
  const regExp = const RegExp("a");
  print(regExp.allMatches("Hello Jordania").length); // 2
}
Bargain answered 1/11, 2012 at 14:16 Comment(0)
M
3
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);
}
Maeganmaelstrom answered 23/12, 2019 at 15:15 Comment(1)
Add explanations to your answer.Enterprising
A
2

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
}
Algetic answered 28/10, 2012 at 18:5 Comment(0)
S
1

Probably not the best one, but you can also do something like this.

void main() {
  var a = "ht://adfd//.coma/";

  int count = a.split("t").length - 1;
  print(count);
}

You will need to subtract 1 because it is splitting into a list first and then counting the size of that list.

Sayyid answered 18/5, 2021 at 12:50 Comment(0)
M
-1
  // count letters
  Map<int,int> counts = new Map<int,int>();
  for (var c in s.charCodes() ) {
    counts[c] = counts.putIfAbsent(c, p()=>0) + 1;
  }
  // print counts
  for (var c in counts.getKeys())
    print("letter ${new String.fromCharCodes([c])} count=${counts[c]}");
Milligan answered 28/10, 2012 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.