StringPosition["string","sub"]
gives a list of the starting and ending character positions at which "sub" appears as a substring of "string".
StringPosition["string",patt]
gives all positions at which substrings matching the general string expression patt appear in "string".
StringPosition["string",patt,n]
includes only the first n occurrences of patt.
StringPosition["string",{patt1,patt2,…}]
gives positions of all the patti.
StringPosition[{s1,s2,…},p]
gives the list of results for each of the si.
StringPosition[patt]
represents an operator form of StringPosition that can be applied to an expression.
StringPosition
StringPosition["string","sub"]
gives a list of the starting and ending character positions at which "sub" appears as a substring of "string".
StringPosition["string",patt]
gives all positions at which substrings matching the general string expression patt appear in "string".
StringPosition["string",patt,n]
includes only the first n occurrences of patt.
StringPosition["string",{patt1,patt2,…}]
gives positions of all the patti.
StringPosition[{s1,s2,…},p]
gives the list of results for each of the si.
StringPosition[patt]
represents an operator form of StringPosition that can be applied to an expression.
Details and Options
- The string expression patt can contain any of the objects specified in the notes for StringExpression.
- StringPosition takes the following options:
-
IgnoreCase False whether to turn off case-sensitive matching Overlaps True which overlapping matches to include - With the default option setting Overlaps->True, StringPosition includes substrings that overlap. With the setting Overlaps->False, such substrings are excluded. »
- With Overlaps->All, multiple substrings that match the same string expression are all included. With Overlaps->True, only the first such matching substring at a given position is included. »
- Setting the option IgnoreCase->True makes StringPosition treat lowercase and uppercase letters as equivalent. »
- StringPosition returns sequence specifications in the form used by StringTake, StringDrop, and StringReplacePart. »
- StringPosition["string",RegularExpression["regex"]] gives positions of substrings matching the specified regular expression.
- StringPosition[patt][expr] is equivalent to StringPosition[expr, patt].
- StringPosition[BioSequence["type","seq"],patt,…] searches the string "seq" for occurrences of patt. 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 StringPosition is circular, wraparound matches are possible. As in StringTake, StringDrop and StringReplacePart, wraparound matches have the form {m,n} with m>n.
Examples
open all close allBasic Examples (3)
Find the starting and ending positions at which "XYZ" occurs in a string:
StringPosition["abXYZaaabXYZaaaaXYZXYZ", "XYZ"]Find where any pairs of identical characters occur:
StringPosition["AABBBAABABBCCCBAAA", x_ ~~ x_]Use the operator form of StringPosition:
StringPosition["XYZ"]["abXYZaaabXYZaaaaXYZXYZ"]Scope (7)
StringPosition gives substring positions in the form used by functions like StringTake:
StringPosition["XYZabc", "XYZ"]StringTake["XYZabc", {1, 3}]Find the position of the first occurrence of "XYZ":
StringPosition["abXYZaaabXYZaaaaXYZXYZ", "XYZ", 1]StringPosition by default includes overlaps:
StringPosition["AAAAA", "AA"]Use Overlaps->False to exclude overlaps:
StringPosition["AAAAA", "AA", Overlaps -> False]StringPosition by default includes overlaps of patterns:
StringPosition["ABAABBAABABB", {"ABA", "AA"}]Use Overlaps->False to exclude overlaps of patterns:
StringPosition["ABAABBAABABB", {"ABA", "AA"}, Overlaps -> False]Find the position of a subsequence within a DNA sequence:
StringPosition[BioSequence["DNA", "GTCTCGCGTAGC"], "TCG"]Use a wildcard in the pattern found in a given biomolecular sequence:
StringPosition[BioSequence["DNA", "ATGGCGG"], "GGN"]The "N" is a degenerate letter and is not a wildcard except in biomolecular sequences:
StringPosition["ATGGCGG", "GGN"]Additional wraparound matches may be found in circular biomolecular sequences:
StringPosition[BioSequence["CircularDNA", "ATGGCGG"], "GGN"]Match only literal degenerate letter occurrences using Verbatim:
StringPosition[BioSequence["DNA", "ATGGCGGN"], "GG" ~~ Verbatim["N"]]Options (5)
IgnoreCase (2)
Treat "a" and "A" as the same:
StringPosition["abAB", "a", IgnoreCase -> True]Treat "a" and "A" as different:
StringPosition["abAB", "a", IgnoreCase -> False]Find successive identical characters independent of case:
StringPosition["aAbaBabBBaABAaBa", x_ ~~ x_, IgnoreCase -> True]StringPosition["aAbaBabBBaABAaBa", x_ ~~ x_, IgnoreCase -> False]Overlaps (3)
StringPosition by default includes overlaps:
StringPosition["AABBBAABABBCCCBAAA", x_ ~~ x_]StringPosition["AABBBAABABBCCCBAAA", x_ ~~ x_, Overlaps -> False]By default, StringPosition only includes one substring starting at any given position:
StringPosition["AAAA", x__]With Overlaps->All, it includes all substrings:
StringPosition["AAAA", x__, Overlaps -> All]StringPosition["AAAA", {x__, "A"}]Find positions of subsequences in a circular DNA sequence, including overlaps:
StringPosition[BioSequence["CircularDNA", "GATTGGTTTGCTTCATTCGTT", {}], "TTNNTT"]Do not allow overlaps between the subsequences:
StringPosition[BioSequence["CircularDNA", "GATTGGTTTGCTTCATTCGTT", {}], "TTNNTT", Overlaps -> False]Applications (2)
Find positions of pairs of identical letters in a concatenation sequence:
StringJoin[Tuples[{"A", "B"}, 4]]First /@ StringPosition[%, x_ ~~ x_]ListPlot[%]Find an analogous curve for triples:
ListPlot[First /@ StringPosition[StringJoin[Tuples[{"A", "B"}, 7]], x_ ~~ x_ ~~ x_]]Find the positions of the word "president" in the US Constitution:
data = ExampleData[{"Text", "USConstitution"}];ListPlot[First /@ StringPosition[data, "president", IgnoreCase -> True]]Properties & Relations (2)
Use StringTake to extract substrings found by StringPosition:
StringPosition["AABBBAABABBCCCBAAA", x_ ~~ x_]StringTake["AABBBAABABBCCCBAAA", #]& /@ %StringCases by default excludes overlaps:
StringCases["AABBBAABABBCCCBAAA", x_ ~~ x_]StringCases["AABBBAABABBCCCBAAA", x_ ~~ x_, Overlaps -> True]Use StringReplacePart to replace at positions found by StringPosition:
StringPosition["abXYZaaabXYZaaaaXYZXYZ", "XYZ"]StringReplacePart["abXYZaaabXYZaaaaXYZXYZ", "--", %]Possible Issues (1)
Lists of string patterns in StringPosition are sometimes not the same as pattern alternatives:
StringPosition["AAAA", {x__, "A"}]StringPosition["AAAA", StringExpression[{x__, "A"}]]StringPosition["AAAA", x__ | "A"]Tech Notes
Related Guides
Related Links
History
Introduced in 1991 (2.0) | Updated in 2003 (5.0) ▪ 2004 (5.1) ▪ 2016 (10.4) ▪ 2020 (12.2)
Text
Wolfram Research (1991), StringPosition, Wolfram Language function, https://reference.wolfram.com/language/ref/StringPosition.html (updated 2020).
CMS
Wolfram Language. 1991. "StringPosition." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringPosition.html.
APA
Wolfram Language. (1991). StringPosition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringPosition.html
BibTeX
@misc{reference.wolfram_2026_stringposition, author="Wolfram Research", title="{StringPosition}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringPosition.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringposition, organization={Wolfram Research}, title={StringPosition}, year={2020}, url={https://reference.wolfram.com/language/ref/StringPosition.html}, note=[Accessed: 15-June-2026]}