StringContainsQ["string",patt]
yields True if any substring in "string" matches the string expression patt, and yields False otherwise.
StringContainsQ["string",{patt1,patt2,…}]
yields True if any substring matches any of the patti.
StringContainsQ[{"string1","string2",…},patt]
gives a list of the results for each of the "stringi".
StringContainsQ[patt]
represents an operator form of StringContainsQ that can be applied to an expression.
StringContainsQ
StringContainsQ["string",patt]
yields True if any substring in "string" matches the string expression patt, and yields False otherwise.
StringContainsQ["string",{patt1,patt2,…}]
yields True if any substring matches any of the patti.
StringContainsQ[{"string1","string2",…},patt]
gives a list of the results for each of the "stringi".
StringContainsQ[patt]
represents an operator form of StringContainsQ 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.
- StringContainsQ takes the following option:
-
IgnoreCase False whether to turn off case-sensitive matching - Setting the option IgnoreCase->True makes StringContainsQ treat lowercase and uppercase letters as equivalent.
- StringContainsQ[patt][expr] is equivalent to StringContainsQ[expr,patt].
- StringConstainsQ[BioSequence["type","seq"],patt] checks the string "seq" against 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 StringContainsQ is circular, wraparound matches are possible.
Examples
open all close allBasic Examples (3)
Test whether a string contains a substring matching the pattern:
StringContainsQ["abcd", "b"]StringContainsQ["bcde", "c" ~~ __ ~~ "t"]StringContainsQ["bcde", "b" ~~ __ ~~ "e"]Give a list of strings as input:
StringContainsQ[{"a", "b", "ab", "abcd", "bcde"}, "a"]Use the operator form of StringContainsQ:
StringContainsQ["a"]["abcd"]Use the operator form to select strings:
Select[{"CAC1", "CTG1", "ACT1", "CGA1", "CTC1"}, StringContainsQ["G"]]Scope (10)
StringContainsQ["abcade", x_ ~~ x_]StringContainsQ["a1 and a2", DigitCharacter..]StringContainsQ["abcde", RegularExpression["b.*d"]]Mix regular expressions and string patterns:
StringContainsQ["bac 123", RegularExpression["a.*"] ~~ DigitCharacter..]Use pattern matching for dates:
StringContainsQ["31/12/2003", DatePattern[{"Day", "Month", "Year"}]]Test for occurrences of either pattern:
StringContainsQ["abcdabcdcd", {"abc", "cd"}]StringContainsQ["abcdabcdcd", "abc" | "cd"]StringContainsQ automatically threads over lists of strings:
StringContainsQ[{"ability", "listable", "argument"}, "a" ~~ __ ~~ "t" ~~ ___]Check whether a biomolecular sequence contains a particular substring:
StringContainsQ[BioSequence["DNA", "CGGTGGAAC"], "TGG"]Use a wildcard in the pattern compared against a biomolecular sequence:
StringContainsQ[BioSequence["DNA", "ATGGC"], "GNC"]The "N" is a degenerate letter only in biomolecular sequences:
StringContainsQ["ATGGC", "GNC"]Check for literal degenerate letters using Verbatim:
StringContainsQ[BioSequence["DNA", "ATGGC"], "G" ~~ Verbatim["N"] ~~ "C"]StringContainsQ[BioSequence["DNA", "ATGNC"], "G" ~~ Verbatim["N"] ~~ "C"]Circular sequences support wraparound checks:
StringContainsQ[BioSequence["CircularDNA", "ATGGC"], "CA"]StringContainsQ[BioSequence["CircularDNA", "ATGGC"], "CT"]StringContainsQ[BioSequence["CircularDNA", "ATGGC"], "CN"]Options (2)
IgnoreCase (2)
Use IgnoreCaseTrue to check for the presence of substrings regardless of case:
StringContainsQ["abcd", "BC", IgnoreCase -> False]StringContainsQ["abcd", "BC", IgnoreCase -> True]The operator representation of a case-independent check:
StringContainsQ["ac", IgnoreCase -> True]["BACCD"]Applications (1)
Pick all the lines that contain a substring that matches the pattern:
grep[file_, patt_] := With[{data = Import[file, "Lines"]}, Pick[Transpose[{Range[Length[data]], data}], StringContainsQ[data, patt]]]Line numbers with corresponding texts that contain "noon" or "day of":
grep["ExampleData/USConstitution.txt", {"noon", "day of"}]//TableFormProperties & Relations (9)
StringContainsQ threads over lists of strings in the first argument:
StringContainsQ[{"aba", "bcd", "cea"}, "a"]StringContainsQ["string",{patt1,patt2,…}] is equivalent to StringContainsQ["string",Alternatives[patt1,patt2,…]]:
StringContainsQ["cad", {"a", "b"}]StringContainsQ["cad", "a" | "b"]StringFreeQ["string",patt] is equivalent to !StringContainsQ["string",patt]:
StringFreeQ["commit", "co"]StringContainsQ["commit", "co"]Use StringMatchQ to check whether a whole string matches a pattern:
StringMatchQ["commit", "om" ~~ __]StringContainsQ checks whether a substring matches a pattern:
StringContainsQ["commit", "om" ~~ __]StringContainsQ generally returns True when StringMatchQ is True:
StringMatchQ["commit", "co" ~~ __]StringContainsQ["commit", "co" ~~ __]StringContainsQ["string",patt] is equivalent to StringMatchQ["string",___~~patt~~___]:
StringContainsQ["commit", "om"]StringMatchQ["commit", ___ ~~ "om" ~~ ___]StringStartsQ["string",patt] is equivalent to StringContainsQ["string",StartOfString~~patt]:
StringStartsQ["commit", "co"]StringContainsQ["commit", StartOfString ~~ "co"]StringEndsQ["string",patt] is equivalent to StringContainsQ["string",patt~~EndOfString]:
StringEndsQ["commit", "it"]StringContainsQ["commit", "it" ~~ EndOfString]Get the starting and ending positions where a substring occurs by using StringPosition:
StringContainsQ["agaatcgagttgacacgaccgaaaacgacc", x_ ~~ x_]StringPosition["agaatcgagttgacacgaccgaaaacgacc", x_ ~~ x_]Use StringCases to extract substrings matching a given pattern:
StringContainsQ["abcadcacb", "a" ~~ x_ ~~ "c"]StringCases["abcadcacb", "a" ~~ x_ ~~ "c"]See Also
StringFreeQ StringCount StringCases StringPosition StringMatchQ StringStartsQ StringEndsQ StringDelete StringExpression RegularExpression
Function Repository: StringContainsAll
Tech Notes
Related Guides
Text
Wolfram Research (2015), StringContainsQ, Wolfram Language function, https://reference.wolfram.com/language/ref/StringContainsQ.html (updated 2020).
CMS
Wolfram Language. 2015. "StringContainsQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringContainsQ.html.
APA
Wolfram Language. (2015). StringContainsQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringContainsQ.html
BibTeX
@misc{reference.wolfram_2026_stringcontainsq, author="Wolfram Research", title="{StringContainsQ}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringContainsQ.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringcontainsq, organization={Wolfram Research}, title={StringContainsQ}, year={2020}, url={https://reference.wolfram.com/language/ref/StringContainsQ.html}, note=[Accessed: 13-June-2026]}