({{, (RefLink[StringPosition, paclet:ref/StringPosition])["string","sub"]
gives a list of the starting and ending character positions at which "sub" appears as a substring of "string". }, {, (RefLink[StringPosition, paclet:ref/StringPosition])["string",patt]
gives all positions at which substrings matching the general string expression patt appear in "string". }, {, (RefLink[StringPosition, paclet:ref/StringPosition])["string",patt,n]
includes only the first n occurrences of patt. }, {, (RefLink[StringPosition, paclet:ref/StringPosition])["string",{(Subscript[patt, 1]),(Subscript[patt, 2]),…}]
gives positions of all the (Subscript[patt, i]). }, {, (RefLink[StringPosition, paclet:ref/StringPosition])[{(Subscript[s, 1]),(Subscript[s, 2]),…},p]
gives the list of results for each of the (Subscript[s, i]). }, {, (RefLink[StringPosition, paclet:ref/StringPosition])[patt]
represents an operator form of (RefLink[StringPosition, paclet:ref/StringPosition]) that can be applied to an expression.}})

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[True, paclet:ref/True]), (RefLink[StringPosition, paclet:ref/StringPosition]) includes substrings that overlap. With the setting (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[False, paclet:ref/False]), such substrings are excluded. »

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

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

(RefLink[StringPosition, paclet:ref/StringPosition]) returns sequence specifications in the form used by (RefLink[StringTake, paclet:ref/StringTake]), (RefLink[StringDrop, paclet:ref/StringDrop]), and (RefLink[StringReplacePart, paclet:ref/StringReplacePart]). »

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

(RefLink[StringPosition, paclet:ref/StringPosition])[patt][expr] is equivalent to (RefLink[StringPosition, paclet:ref/StringPosition])[expr, patt].

OpenersPrimaryExamplesSection(Spacer1)Examples  (16)

OpenersExampleSection0(Spacer1)Basic 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 (RefLink[StringPosition, paclet:ref/StringPosition]):

StringPosition["XYZ"]["abXYZaaabXYZaaaaXYZXYZ"]

OpenersExampleSection1(Spacer1)Scope  (4)

(RefLink[StringPosition, paclet:ref/StringPosition]) gives substring positions in the form used by functions like (RefLink[StringTake, paclet:ref/StringTake]):

StringPosition["XYZabc","XYZ"]

StringTake["XYZabc",{1,3}]

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

StringPosition["abXYZaaabXYZaaaaXYZXYZ","XYZ",1]

(RefLink[StringPosition, paclet:ref/StringPosition]) by default includes overlaps:

StringPosition["AAAAA","AA"]

Use (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[False, paclet:ref/False]) to exclude overlaps:

StringPosition["AAAAA","AA",Overlaps->False]

StringPosition["ABAABBAABABB",{"ABA","AA"}]

StringPosition["ABAABBAABABB",{"ABA","AA"},Overlaps->False]

OpenersExampleSection2(Spacer1)Options  (4)

OpenersExampleSubsection0(Spacer1)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]

OpenersExampleSubsection1(Spacer1)Overlaps  (2)

(RefLink[StringPosition, paclet:ref/StringPosition]) by default includes overlaps:

StringPosition["AABBBAABABBCCCBAAA",x_~~x_]

StringPosition["AABBBAABABBCCCBAAA",x_~~x_,Overlaps->False]

By default, (RefLink[StringPosition, paclet:ref/StringPosition]) only includes one substring starting at any given position:

StringPosition["AAAA",x__]

With (RefLink[Overlaps, paclet:ref/Overlaps])->(RefLink[All, paclet:ref/All]), it includes all substrings:

StringPosition["AAAA",x__,Overlaps->All]

StringPosition["AAAA",{x__,"A"}]

OpenersExampleSection3(Spacer1)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]]

OpenersExampleSection4(Spacer1)Properties & Relations  (2)

Use (RefLink[StringTake, paclet:ref/StringTake]) to extract substrings found by (RefLink[StringPosition, paclet:ref/StringPosition]):

StringPosition["AABBBAABABBCCCBAAA",x_~~x_]

StringTake["AABBBAABABBCCCBAAA",#]&/@%

(RefLink[StringCases, paclet:ref/StringCases]) by default excludes overlaps:

StringCases["AABBBAABABBCCCBAAA",x_~~x_]

StringCases["AABBBAABABBCCCBAAA",x_~~x_,Overlaps->True]

Use (RefLink[StringReplacePart, paclet:ref/StringReplacePart]) to replace at positions found by (RefLink[StringPosition, paclet:ref/StringPosition]):

StringPosition["abXYZaaabXYZaaaaXYZXYZ","XYZ"]

StringReplacePart["abXYZaaabXYZaaaaXYZXYZ","--",%]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

Lists of string patterns in (RefLink[StringPosition, paclet:ref/StringPosition]) are sometimes not the same as pattern alternatives:

StringPosition["AAAA",{x__,"A"}]

StringPosition["AAAA",StringExpression[{x__,"A"}]]

StringPosition["AAAA",x__|"A"]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[TextPosition, paclet:ref/TextPosition])  ▪  (RefLink[Position, paclet:ref/Position])  ▪  (RefLink[StringCases, paclet:ref/StringCases])  ▪  (RefLink[StringCount, paclet:ref/StringCount])  ▪  (RefLink[StringContainsQ, paclet:ref/StringContainsQ])  ▪  (RefLink[Characters, paclet:ref/Characters])  ▪  (RefLink[FindList, paclet:ref/FindList])  ▪  (RefLink[ReplaceList, paclet:ref/ReplaceList])  ▪  (RefLink[StringExpression, paclet:ref/StringExpression])  ▪  (RefLink[RegularExpression, paclet:ref/RegularExpression])  ▪  (RefLink[StringDrop, paclet:ref/StringDrop])  ▪  (RefLink[StringInsert, paclet:ref/StringInsert])  ▪  (RefLink[StringTake, paclet:ref/StringTake])  ▪  (RefLink[SequenceAlignment, paclet:ref/SequenceAlignment])}})}})

({{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[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[Sequence Alignment & Comparison, paclet:guide/SequenceAlignmentAndComparison])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=StringPosition]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})