Function Repository Resource:

StringFunction

Source Notebook

Use functions meant for lists on strings

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["StringFunction"][f][string,]

applies f to the characters in string.

Details and Options

The function f should return a string or a list of strings when given a list of characters.
If f returns a list, the output is joined into a single string.
ResourceFunction["StringFunction"][f] is automatically threaded over lists, as if it had the Listable attribute.

Examples

Basic Examples (7) 

Use a function meant for lists on a string:

In[1]:=
ResourceFunction["StringFunction"][Reverse]["Hello World"]
Out[1]=

Create new string functions from existing functions:

In[2]:=
MyStringSelect = ResourceFunction["StringFunction"][Select]
Out[2]=
In[3]:=
MyStringSelect["Hello World", LowerCaseQ]
Out[3]=

Rotate a string:

In[4]:=
ResourceFunction["StringFunction"][RotateLeft]["Hello World"]
Out[4]=
In[5]:=
ResourceFunction["StringFunction"][RotateRight]["Hello World"]
Out[5]=

Get a random permutation of a string:

In[6]:=
ResourceFunction["StringFunction"][RandomSample]["Hello World"]
Out[6]=

Replace all vowels:

In[7]:=
ResourceFunction["StringFunction"][
  ReplaceAll["a" | "e" | "i" | "o" | "u" -> "X"]]["Hello World"]
Out[7]=

Insert spaces between each letter:

In[8]:=
ResourceFunction["StringFunction"][StringRiffle]["Hello World"]
Out[8]=

More examples for StringFunction:

In[9]:=
ResourceFunction["StringFunction"][Delete]["abcde", 3]
Out[9]=
In[10]:=
ResourceFunction["StringFunction"][Drop]["don't do it!", 6]
Out[10]=
In[11]:=
ResourceFunction["StringFunction"][Union]["this is a string"]
Out[11]=
In[12]:=
ResourceFunction["StringFunction"][
  DeleteDuplicates]["this is a string"]
Out[12]=
In[13]:=
ResourceFunction["StringFunction"][Sort]["this is a string"]
Out[13]=
In[14]:=
ResourceFunction["StringFunction"][Permutations]["abcd"]
Out[14]=
In[15]:=
ResourceFunction["StringFunction"][Subsets]["string"]
Out[15]=
In[16]:=
ResourceFunction["StringFunction"][Gather]["this is a string"]
Out[16]=

Scope (4) 

Functions created by StringFunction behave as though they were Listable:

In[17]:=
MyStringReverse = ResourceFunction["StringFunction"][Reverse]
Out[17]=
In[18]:=
MyStringReverse[{"cat", "dog", "fish", "coelenterate"}]
Out[18]=

This is similar to many of the built-in string functions:

In[19]:=
StringReverse[{"cat", "dog", "fish", "coelenterate"}]
Out[19]=

Use a custom function in StringFunction:

In[20]:=
randomStartsWith[c_] := RandomChoice[DictionaryLookup[c ~~ __]] <> "! ";
standsFor = Row[{#, " stands for:\n", ResourceFunction["StringFunction"][Map[randomStartsWith]][#]}] &
Out[18]=
In[21]:=
standsFor["string"]
Out[21]=

If the given function produces nested lists of strings, the list structure will be preserved:

In[22]:=
ResourceFunction["StringFunction"][Outer[List, #, #] &]["ooer!"]
Out[22]=

Characters are separated using Characters instead of byte values, so encoding is preserved:

In[23]:=
ResourceFunction["StringFunction"][StringRiffle@*CharacterName]["猫鳥犬"]
Out[23]=

Properties and Relations (2) 

The given function is applied to a list of strings:

In[24]:=
ResourceFunction["StringFunction"][ToString@*ListQ]["Hello World"]
Out[24]=
In[25]:=
ResourceFunction["StringFunction"][ToString@*StringQ]["Hello World"]
Out[25]=

StringFunction applies f to Characters["string"] and joins the result:

In[26]:=
ResourceFunction["StringFunction"][Echo]["Hello World"]
Out[26]=
In[27]:=
ResourceFunction["StringFunction"][Map[Echo]]["abc"]
Out[27]=

Possible Issues (2) 

The given f must produce a string or lists of strings from its output:

In[28]:=
ResourceFunction["StringFunction"][ToCharacterCode]["abc"]
Out[28]=
In[29]:=
ResourceFunction["StringFunction"][
  StringRiffle@*Map[ToString]@*Flatten@*ToCharacterCode]["abc"]
Out[29]=

Validity is not determined until evaluation of StringFunction[][args]:

In[30]:=
f = ResourceFunction["StringFunction"][invalid]
Out[30]=
In[31]:=
f["oh no!"]
Out[31]=

Neat Examples (1) 

Create a scrolling marquee to experience that ‘90s website nostalgia:

In[32]:=
scroll[text_String] := ListAnimate[
   NestList[ResourceFunction["StringFunction"][RotateLeft], text <> " ", StringLength[text]]];
In[33]:=
Style[scroll["Welcome to my website about cats!"], FontFamily -> "Comic Sans MS", FontSize -> 24, FontColor -> Purple]
Out[33]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 08 February 2019

Related Resources

License Information