Function Repository Resource:

StringSelect

Source Notebook

Select characters from strings

Contributed by: Maximilien Tirard

ResourceFunction["StringSelect"][str,crit]

picks out all characters of str for which crit[ci] is True.

ResourceFunction["StringSelect"][str,critprop]

returns the property prop of the selected elements.

ResourceFunction["StringSelect"][str,crit,n]

picks out the first n elements for which crit[ei] is True.

ResourceFunction["StringSelect"][crit]

represents an operator form of StringSelect that can be applied to an expression.

Details

The property prop can have the following forms and interpretations:
"Element"the selected characters
"Index"indices of the selected characters
"BitVectorMask"Boolean mask returning True for selected values and False otherwise
{prop1,prop2}a list of multiple forms
Allan association giving element, index and bit vector mask
SelectString[crit][str] is equivalent to ResourceFunction["StringSelect"][str,crit]

Examples

Basic Examples (5) 

Select only the letters from a sample of the United States Declaration of Independence:

In[1]:=
sampleString = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 100];
ResourceFunction["StringSelect"][sampleString, LetterQ]
Out[2]=

Select only vowels from a sample of the United States Declaration of Independence:

In[3]:=
sampleString = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 100];
vowelQ = MemberQ[Characters["aeiouy"], #] &;
ResourceFunction["StringSelect"][sampleString, vowelQ]
Out[5]=

Select only upper case letters from a sample of the United States Declaration of Independence:

In[6]:=
sampleString = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 2000];
ResourceFunction["StringSelect"][sampleString, UpperCaseQ]
Out[7]=

Select punctuation from a sample of the United States Declaration of Independence:

In[8]:=
sampleString = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 5000];
punctuationQ = StringMatchQ[#, PunctuationCharacter] &;
ResourceFunction["StringSelect"][sampleString, punctuationQ]
Out[10]=

Use the operator form:

In[11]:=
ResourceFunction[
  "StringSelect"][! LetterQ[#] &]["\[OpenCurlyDoubleQuote]I came; I saw; I conquered!\[CloseCurlyDoubleQuote]"]
Out[11]=

Neat Examples (1) 

Get the indices of upper case letters:

In[12]:=
sampleString = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 1000];
ResourceFunction["StringSelect"][sampleString, UpperCaseQ -> "Index"]
Out[13]=

Publisher

Maximilien Tirard

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 21 March 2025

Related Resources

License Information