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

Examples

open allclose all

Basic Examples  (3)

Find the starting and ending positions at which "XYZ" occurs in a string:

Find where any pairs of identical characters occur:

Use the operator form of StringPosition:

Scope  (7)

StringPosition gives substring positions in the form used by functions like StringTake:

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

StringPosition by default includes overlaps:

Use Overlaps->False to exclude overlaps:

Find the position of a subsequence within a DNA sequence:

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

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

Additional wraparound matches may be found in circular biomolecular sequences:

Match only literal degenerate letter occurrences using Verbatim:

Options  (5)

IgnoreCase  (2)

Treat "a" and "A" as the same:

Treat "a" and "A" as different:

Find successive identical characters independent of case:

Overlaps  (3)

StringPosition by default includes overlaps:

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

With Overlaps->All, it includes all substrings:

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

Do not allow overlaps between the subsequences:

Applications  (2)

Find positions of pairs of identical letters in a concatenation sequence:

Find an analogous curve for triples:

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

Properties & Relations  (2)

Use StringTake to extract substrings found by StringPosition:

StringCases by default excludes overlaps:

Use StringReplacePart to replace at positions found by StringPosition:

Possible Issues  (1)

Lists of string patterns in StringPosition are sometimes not the same as pattern alternatives:

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).

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_2024_stringposition, author="Wolfram Research", title="{StringPosition}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringPosition.html}", note=[Accessed: 22-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_stringposition, organization={Wolfram Research}, title={StringPosition}, year={2020}, url={https://reference.wolfram.com/language/ref/StringPosition.html}, note=[Accessed: 22-December-2024 ]}