countLetter method

int countLetter(
  1. String targetLetter
)

The countLetter extension counts the occurrences of a specific single letter within a given text here's and Example:

==================================================================>

String longText = "This is an example text with ab letters and ab words. It also contains abbreviations like abc and abcd. The rabbit jumped over the ab fence.";

int count = longText.countLetter("a");

print("The sequence 'a' appears $count times in the text.");

==================================================================>

Implementation

int countLetter(String targetLetter) {
  if (targetLetter.length > 1) {
    throw ErrorDescription(
        "The method failed because you pass more than single letter in targetLetter property");
  }
  return split('').where((char) => char == targetLetter).length;
}