Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Mathematica > Core Language > String Manipulation > String Operations >

StringCount

StringCount["string", "sub"]
gives a count of the number of times "sub" appears as a substring of "string".
StringCount["string", patt]
gives the number of substrings in "string" that match the general string expression patt.
StringCount["string", {patt1, patt2, ...}]
counts the number of occurrences of any of the patti.
StringCount[{s1, s2, ...}, p]
gives the list of results for each of the si.
  • 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.
The number of occurrences of "bb" in the string "abbaabbaa":
Count the number of substrings of the form "axb" for different x characters:
The number of occurrences of "bb" in the string "abbaabbaa":
In[1]:=
Click for copyable input
Out[1]=
 
Count the number of substrings of the form "axb" for different x characters:
In[1]:=
Click for copyable input
Out[1]=
Use string patterns:
Regular expressions:
Mixed regular expressions and string patterns:
Count occurrences of either substrings:
StringCount automatically threads over lists of strings:
The number of occurrences of "a" in "abAB":
Ignore case:
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:
A 10-million-base random DNA string:
The number of sequences with adenine symmetrically placed:
Find how many words occur in the U.S. Constitution:
The number of occurrences of the word "president":
All strings made of two characters with length 4 and which overlap themselves:
StringCount gives the number of matching substrings:
The length of matching substrings obtained from StringCases:
New in 5.1
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team