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.
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.
Details and Options
- The string expression patt can contain any of the objects specified in the notes for StringExpression.
- StringCount takes the following options:
-
IgnoreCase False whether to turn off case-sensitive matching Overlaps False which overlapping matches to include - 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.
- StringCount[BioSequence["type","seq"],patt] counts the matches of patt in the string "seq". In this case, degenerate letters in patt are interpreted as wildcard patterns based on the type of biomolecular sequence. Use Verbatim["patt"] to match degenerate letters literally.
- The documentation for BioSequence lists the degenerate letters supported by each type of biomolecular sequence.
- If the biomolecular sequence operated upon by StringCount is circular, wraparound matches are possible.
Examples
open all close allBasic Examples (2)
Scope (8)
StringCount["the cat in the hat", "cat"]StringCount["the cat in the hat", "a" ~~ __ ~~ "e"]StringCount["11a22b3", DigitCharacter..]StringCount["11a22b3", _ ? LetterQ]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"]StringCount automatically threads over lists of strings:
StringCount[{"ability", "argument", "listable"}, "a" ~~ ___ ~~ "l"]Count codon-length subsequences in a DNA sequence:
StringCount[BioSequence["DNA", "CTGGCGGTGG"], "GG" ~~ ("C" | "T")]Use a wildcard in the pattern counted in a given biomolecular sequence:
StringCount[BioSequence["DNA", "CTGGCGGTGGYGG"], "GGY"]The "Y" is a degenerate letter and not a wildcard except in biomolecular sequences:
StringCount["CTGGCGGTGGYGG", "GGY"]Additional wraparound matches may be found in circular biomolecular sequences:
StringCount[BioSequence["CircularDNA", "CTGGCGGTGGYGG"], "GGY"]Count only literal degenerate letter occurrences using Verbatim:
StringCount[BioSequence["CircularDNA", "CTGGCGGTGGYGG"], Verbatim["GGY"]]Options (3)
IgnoreCase (1)
Overlaps (2)
All substrings in "the cat in the hat" starting and ending with "t":
StringCases["the cat in the hat", "t" ~~ __ ~~ "t", Overlaps -> All]StringCount does not include overlaps by default:
StringCount["the cat in the hat", "t" ~~ __ ~~ "t"]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]Count subsequences in a circular DNA sequence:
StringCount[BioSequence["CircularDNA", "GATTGGTTTGCTTCATTCGTT", {}], "TTNNTT"]Allow overlaps between the subsequences:
StringCount[BioSequence["CircularDNA", "GATTGGTTTGCTTCATTCGTT", {}], "TTNNTT", Overlaps -> True]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&]Properties & Relations (1)
StringCount gives the number of matching substrings:
StringCount["abaababba", "a" ~~ ___ ~~ "b", Overlaps -> All]The length of matching substrings obtained from StringCases:
Length @ StringCases["abaababba", "a" ~~ ___ ~~ "b", Overlaps -> All]See Also
Count StringContainsQ StringFreeQ StringPosition StringCases CharacterCounts Characters StringExpression RegularExpression SequenceCount
Function Repository: KeywordPlot
Tech Notes
History
Introduced in 2004 (5.1) | Updated in 2020 (12.2)
Text
Wolfram Research (2004), StringCount, Wolfram Language function, https://reference.wolfram.com/language/ref/StringCount.html (updated 2020).
CMS
Wolfram Language. 2004. "StringCount." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringCount.html.
APA
Wolfram Language. (2004). StringCount. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringCount.html
BibTeX
@misc{reference.wolfram_2026_stringcount, author="Wolfram Research", title="{StringCount}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringCount.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringcount, organization={Wolfram Research}, title={StringCount}, year={2020}, url={https://reference.wolfram.com/language/ref/StringCount.html}, note=[Accessed: 13-June-2026]}