StringPosition
✖
StringPosition
gives a list of the starting and ending character positions at which "sub" appears as a substring of "string".
gives all positions at which substrings matching the general string expression patt appear in "string".
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.
- 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 allclose allBasic Examples (3)Summary of the most common use cases
Find the starting and ending positions at which "XYZ" occurs in a string:

https://wolfram.com/xid/0i1ik0b9e-czh

Find where any pairs of identical characters occur:

https://wolfram.com/xid/0i1ik0b9e-yv2

Use the operator form of StringPosition:

https://wolfram.com/xid/0i1ik0b9e-39vk2s

Scope (7)Survey of the scope of standard use cases
StringPosition gives substring positions in the form used by functions like StringTake:

https://wolfram.com/xid/0i1ik0b9e-lj5


https://wolfram.com/xid/0i1ik0b9e-oom

Find the position of the first occurrence of "XYZ":

https://wolfram.com/xid/0i1ik0b9e-ott

StringPosition by default includes overlaps:

https://wolfram.com/xid/0i1ik0b9e-epi

Use Overlaps->False to exclude overlaps:

https://wolfram.com/xid/0i1ik0b9e-ltk


https://wolfram.com/xid/0i1ik0b9e-s9z


https://wolfram.com/xid/0i1ik0b9e-ot5

Find the position of a subsequence within a DNA sequence:

https://wolfram.com/xid/0i1ik0b9e-c8opr2

Use a wildcard in the pattern found in a given biomolecular sequence:

https://wolfram.com/xid/0i1ik0b9e-s8aq0l

The "N" is a degenerate letter and is not a wildcard except in biomolecular sequences:

https://wolfram.com/xid/0i1ik0b9e-0doyya

Additional wraparound matches may be found in circular biomolecular sequences:

https://wolfram.com/xid/0i1ik0b9e-vct9sc

Match only literal degenerate letter occurrences using Verbatim:

https://wolfram.com/xid/0i1ik0b9e-4zcb2d

Options (5)Common values & functionality for each option
IgnoreCase (2)
Treat "a" and "A" as the same:

https://wolfram.com/xid/0i1ik0b9e-h0b

Treat "a" and "A" as different:

https://wolfram.com/xid/0i1ik0b9e-yud

Find successive identical characters independent of case:

https://wolfram.com/xid/0i1ik0b9e-cxz


https://wolfram.com/xid/0i1ik0b9e-kmc

Overlaps (3)
StringPosition by default includes overlaps:

https://wolfram.com/xid/0i1ik0b9e-c5h


https://wolfram.com/xid/0i1ik0b9e-wcd

By default, StringPosition only includes one substring starting at any given position:

https://wolfram.com/xid/0i1ik0b9e-o2q

With Overlaps->All, it includes all substrings:

https://wolfram.com/xid/0i1ik0b9e-odd


https://wolfram.com/xid/0i1ik0b9e-oqv

Find positions of subsequences in a circular DNA sequence, including overlaps:

https://wolfram.com/xid/0i1ik0b9e-dpk4k

Do not allow overlaps between the subsequences:

https://wolfram.com/xid/0i1ik0b9e-ctcwhz

Applications (2)Sample problems that can be solved with this function
Find positions of pairs of identical letters in a concatenation sequence:

https://wolfram.com/xid/0i1ik0b9e-i21


https://wolfram.com/xid/0i1ik0b9e-rcf


https://wolfram.com/xid/0i1ik0b9e-2y

Find an analogous curve for triples:

https://wolfram.com/xid/0i1ik0b9e-h3y

Find the positions of the word "president" in the US Constitution:

https://wolfram.com/xid/0i1ik0b9e-nkg

https://wolfram.com/xid/0i1ik0b9e-lwk

Properties & Relations (2)Properties of the function, and connections to other functions
Use StringTake to extract substrings found by StringPosition:

https://wolfram.com/xid/0i1ik0b9e-gy9


https://wolfram.com/xid/0i1ik0b9e-s53

StringCases by default excludes overlaps:

https://wolfram.com/xid/0i1ik0b9e-p3m


https://wolfram.com/xid/0i1ik0b9e-cqa

Use StringReplacePart to replace at positions found by StringPosition:

https://wolfram.com/xid/0i1ik0b9e-b5t


https://wolfram.com/xid/0i1ik0b9e-h32

Possible Issues (1)Common pitfalls and unexpected behavior
Lists of string patterns in StringPosition are sometimes not the same as pattern alternatives:

https://wolfram.com/xid/0i1ik0b9e-on3


https://wolfram.com/xid/0i1ik0b9e-rpr


https://wolfram.com/xid/0i1ik0b9e-wai

Wolfram Research (1991), StringPosition, Wolfram Language function, https://reference.wolfram.com/language/ref/StringPosition.html (updated 2020).
Text
Wolfram Research (1991), StringPosition, Wolfram Language function, https://reference.wolfram.com/language/ref/StringPosition.html (updated 2020).
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.
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
Wolfram Language. (1991). StringPosition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringPosition.html
BibTeX
@misc{reference.wolfram_2025_stringposition, author="Wolfram Research", title="{StringPosition}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringPosition.html}", note=[Accessed: 19-June-2025
]}
BibLaTeX
@online{reference.wolfram_2025_stringposition, organization={Wolfram Research}, title={StringPosition}, year={2020}, url={https://reference.wolfram.com/language/ref/StringPosition.html}, note=[Accessed: 19-June-2025
]}