gives a count of the number of times "sub" appears as a substring of "string".


gives the number of substrings in "string" that match the general string expression patt.


counts the number of occurrences of any of the patti.


gives the list of results for each of the si.

Details and Options

  • The string expression patt can contain any of the objects specified in the notes for StringExpression.
  • With the default option setting Overlaps->False, overlapping substrings are not treated as separate. With the setting Overlaps->True, StringCount counts substrings that overlap as separate.
  • With Overlaps->All, multiple substrings that match the same string expression are all counted as separate. With Overlaps->True, only the first such matching substring at a given position is counted as separate.
  • Setting the option IgnoreCase->True makes StringCount treat lowercase and uppercase letters as equivalent.
  • StringCount["string",RegularExpression["regex"]] gives the number of substrings matching the specified regular expression.


open allclose all

Basic Examples  (2)

The number of occurrences of "bb" in the string "abbaabbaa":

Count the number of substrings of the form "axc" for different x characters:

Scope  (5)

Use string patterns:

Regular expressions:

Mixed regular expressions and string patterns:

Count occurrences of either substrings:

StringCount automatically threads over lists of strings:

Options  (2)

IgnoreCase  (1)

The number of occurrences of "a" in "abAB":

Ignore case:

Overlaps  (1)

All substrings in "the cat in the hat" starting and ending with "t":

StringCount does not include overlaps by default:

This includes the overlaps:

This includes overlaps starting at the same position:

Applications  (3)

A 10-million-base random DNA string:

The number of sequences with adenine symmetrically placed:

Find how many words occur in the US Constitution:

The number of occurrences of the word "president":

All strings made of two characters with length 4 and which overlap themselves:

Properties & Relations  (1)

StringCount gives the number of matching substrings:

The length of matching substrings obtained from StringCases:

Introduced in 2004