({{, (RefLink[StringCount, paclet:ref/StringCount])["string","sub"]
gives a count of the number of times "sub" appears as a substring of "string". }, {, (RefLink[StringCount, paclet:ref/StringCount])["string",patt]
gives the number of substrings in "string" that match the general string expression patt. }, {, (RefLink[StringCount, paclet:ref/StringCount])["string",{(Subscript[patt, 1]),(Subscript[patt, 2]),…}]
counts the number of occurrences of any of the (Subscript[patt, i]). }, {, (RefLink[StringCount, paclet:ref/StringCount])[{(Subscript[s, 1]),(Subscript[s, 2]),…},p]
gives the list of results for each of the (Subscript[s, i]). }})

OpenersNotesSection(Spacer1)Details and Options

The string expression patt can contain any of the objects specified in the notes for (RefLink[StringExpression, paclet:ref/StringExpression]).

With the default option setting (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[False, paclet:ref/False]), overlapping substrings are not treated as separate. With the setting (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[True, paclet:ref/True]), (RefLink[StringCount, paclet:ref/StringCount]) counts substrings that overlap as separate.

With (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[All, paclet:ref/All]), multiple substrings that match the same string expression are all counted as separate. With (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[True, paclet:ref/True]), only the first such matching substring at a given position is counted as separate.

Setting the option (RefLink[IgnoreCase, paclet:ref/IgnoreCase])->(RefLink[True, paclet:ref/True]) makes (RefLink[StringCount, paclet:ref/StringCount]) treat lowercase and uppercase letters as equivalent.

(RefLink[StringCount, paclet:ref/StringCount])["string",(RefLink[RegularExpression, paclet:ref/RegularExpression])["regex"]] gives the number of substrings matching the specified regular expression.

OpenersPrimaryExamplesSection(Spacer1)Examples  (13)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

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

StringCount["abbaabbaa","bb"]

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

StringCount["abcadcadcbaac","a"~~_~~"c"]

OpenersExampleSection1(Spacer1)Scope  (5)

Use string patterns:

StringCount["the cat in the hat","cat"]

StringCount["the cat in the hat","a"~~__~~"e"]

StringCount["11a22b3",DigitCharacter..]

StringCount["11a22b3",_?LetterQ]

Regular expressions:

StringCount["a1b22c333",RegularExpression["..2"]]

Mixed regular expressions and string patterns:

StringCount["the cat in the hat",RegularExpression["(?<=the )"]~~WordCharacter..]

Count occurrences of either substrings:

StringCount["abcdabcdcd",{"abc","cd"}]

StringCount["abcdabcdcd","abc"|"cd"]

(RefLink[StringCount, paclet:ref/StringCount]) automatically threads over lists of strings:

StringCount[{"ability","argument","listable"},"a" ~~___~~"l"]

OpenersExampleSection2(Spacer1)Options  (2)

OpenersExampleSubsection0(Spacer1)IgnoreCase  (1)

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

StringCount["abAB","a"]

Ignore case:

StringCount["abAB","a",IgnoreCase->True]

OpenersExampleSubsection1(Spacer1)Overlaps  (1)

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

StringCases["the cat in the hat","t"~~__~~"t",Overlaps->All]

(RefLink[StringCount, paclet:ref/StringCount]) does not include overlaps by default:

StringCount["the cat in the hat","t"~~__~~"t"]

This includes the overlaps:

StringCount["the cat in the hat","t"~~__~~"t",Overlaps->True]

This includes overlaps starting at the same position:

StringCount["the cat in the hat","t"~~__~~"t",Overlaps->All]

OpenersExampleSection3(Spacer1)Applications  (3)

A 10-million-base random DNA string:

SeedRandom[1234];
dna = StringJoin[RandomChoice[{"a","c","g","t"},10^7]];

The number of sequences with adenine symmetrically placed:

StringCount[dna,"a"~~y_~~_~~y_~~"a"]

Find how many words occur in the US Constitution:

StringCount[Import["ExampleData/USConstitution.txt"],WordCharacter..]

The number of occurrences of the word "president":

StringCount[Import["ExampleData/USConstitution.txt"],"president",IgnoreCase->True]

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

StringJoin/@Tuples[{"A","B"},4]

Select[%,StringCount[#~~#,#,Overlaps->True]>2&]

OpenersExampleSection4(Spacer1)Properties & Relations  (1)

(RefLink[StringCount, paclet:ref/StringCount]) gives the number of matching substrings:

StringCount["abaababba","a"~~___~~"b",Overlaps->All]

The length of matching substrings obtained from (RefLink[StringCases, paclet:ref/StringCases]):

Length @ StringCases["abaababba","a"~~___~~"b",Overlaps->All]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Count, paclet:ref/Count])  ▪  (RefLink[StringContainsQ, paclet:ref/StringContainsQ])  ▪  (RefLink[StringFreeQ, paclet:ref/StringFreeQ])  ▪  (RefLink[StringPosition, paclet:ref/StringPosition])  ▪  (RefLink[StringCases, paclet:ref/StringCases])  ▪  (RefLink[CharacterCounts, paclet:ref/CharacterCounts])  ▪  (RefLink[Characters, paclet:ref/Characters])  ▪  (RefLink[StringExpression, paclet:ref/StringExpression])  ▪  (RefLink[RegularExpression, paclet:ref/RegularExpression])  ▪  (RefLink[SequenceCount, paclet:ref/SequenceCount])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Operations on Strings, paclet:tutorial/OperationsOnStrings])}, {▪(RefLinkPlain[Special Characters, paclet:tutorial/SpecialCharacters-StringsAndCharacters])}, {▪(RefLinkPlain[String Patterns, paclet:tutorial/StringPatterns])}, {▪(RefLinkPlain[Working with String Patterns, paclet:tutorial/WorkingWithStringPatternsOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[String Operations, paclet:guide/StringOperations])}, {▪(RefLinkPlain[Scientific Data Analysis, paclet:guide/ScientificDataAnalysis])}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[Linguistic Data, paclet:guide/LinguisticData])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[Sequence Alignment & Comparison, paclet:guide/SequenceAlignmentAndComparison])}})}})