LetterCounts

LetterCounts["string"]

gives an association whose keys are the distinct letters in string, and whose values give the number of times those letters appear in string.

LetterCounts["string",n]

gives counts of the distinct n-grams consisting of runs of n letters in string.

LetterCounts["string",n,{"c1","c2",}]

allows the characters ci to appear in n-grams, in addition to ordinary letters.

Details and Options

  • LetterCounts[string,n] considers n-grams that start at every position in string and includes them in its counts if they contain only letters.
  • LetterCounts effectively uses LetterQ to determine whether to consider a character to be a letter.
  • LetterCounts has the option IgnoreCase. With the setting IgnoreCase->True, letters are in effect all converted to lower case before being counted.

Examples

open allclose all

Basic Examples  (4)

Count the letters in a string:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Non-letter characters are excluded from letter counts:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Count 2-grams of letters:

In[1]:=
Click for copyable input
Out[1]=

Specify a list of non-letter characters to be counted:

In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

Options  (2)

Neat Examples  (1)

See Also

CharacterCounts  StringLength  WordCounts  Counts  KeySort  Sort  WordCloud  LetterQ

Introduced in 2015
(10.1)