Function Repository Resource:

StringContainsAll

Source Notebook

Test if a string contains all elements from a list

Contributed by: Jason Biggs

ResourceFunction["StringContainsAll"][string,{patt1,patt2,}]

returns True if string contains each of the patti.

ResourceFunction["StringContainsAll"][{patt1,patt2,}][string]

is an operator form of ResourceFunction["StringContainsAll"] that can be applied to arguments.

Details and Options

For fastest results, order the patti with the least common pattern first.
ResourceFunction["StringContainsAll"] takes the same options as StringContainsQ.

Examples

Basic Examples (2) 

Bring up the opening of the Declaration of Independence:

In[1]:=
string = StringTake[
  ExampleData[{"Text", "DeclarationOfIndependence"}], 400]
Out[1]=

Test to see whether the text contains the words "people", "dissolve" and "Laws":

In[2]:=
ResourceFunction[
 "StringContainsAll"][string, {"people", "dissolve", "Laws"}]
Out[2]=

Conversely, despite containing the word "human", the text does not contain all of the words "human" and "xylophone":

In[3]:=
StringContainsQ[string, "human"]
Out[3]=
In[4]:=
ResourceFunction["StringContainsAll"][string, {"human", "xylophone"}]
Out[4]=

Define some text:

In[5]:=
string2 = StringTake[ExampleData[{"Text", "OriginOfSpecies"}], 400]
Out[5]=

Use the operator form of StringContainsAll:

In[6]:=
ResourceFunction[
  "StringContainsAll"][{"naturalist", "inhabitants"}][string2]
Out[6]=
In[7]:=
ResourceFunction[
  "StringContainsAll"][{"mystery", "evolution"}][string2]
Out[7]=

Scope (1) 

Find all words containing "e", "t", "l", "a" and "b" and put them in a WordCloud:

In[8]:=
WordCloud@
 Select[DictionaryLookup[], ResourceFunction["StringContainsAll"][{"e", "t", "l", "a", "b"}]]
Out[8]=

Options (2) 

String patterns are case sensitive:

In[9]:=
string = StringTake[ExampleData[{"Text", "TheRaven"}], 400]
Out[9]=
In[10]:=
ResourceFunction["StringContainsAll"][string, {"dreary", "december"}]
Out[10]=

Use the option IgnoreCase to match upper- and lower-case letters:

In[11]:=
ResourceFunction["StringContainsAll"][string, {"dreary", "december"}, IgnoreCase -> True]
Out[11]=

Publisher

JasonB

Version History

  • 1.0.0 – 27 April 2020

License Information