Function Repository Resource:

PhraseRiffle

Source Notebook

Convert a list of strings into a phrase

Contributed by: Gustavo Delfino

ResourceFunction["PhraseRiffle"][{s1,s2,s3,}]

creates a phrase by concatenating all the si using English separators.

ResourceFunction["PhraseRiffle"][{s1,s2,s3,},fsep]

uses fsep as a custom final separator string.

ResourceFunction["PhraseRiffle"][{s1,s2,s3,},fsep,sep]

uses sep as a custom separator and fsep for the final pair only.

ResourceFunction["PhraseRiffle"][fsep][{s1,s2,s3,}]

operator form with custom final separator fsep.

ResourceFunction["PhraseRiffle"][fsep,sep][{s1,s2,s3,}]

operator form with custom final and common separators.

Details

The default separator is a comma followed by a space.
The default fsep is " and " (note the spaces before and after).

Examples

Basic Examples (4) 

Convert a list of strings into a phrase in English:

In[1]:=
ResourceFunction["PhraseRiffle"][{"a", "b", "c", "d", "e"}]
Out[1]=

For other languages, use a different final separator:

In[2]:=
ResourceFunction["PhraseRiffle"][{"a", "b", "c", "d", "e"}, " und "]
Out[2]=

The main separator is also customizable:

In[3]:=
ResourceFunction[
 "PhraseRiffle"][{"a", "b", "c", "d", "e"}, " y ", "; "]
Out[3]=

An operator form is supported:

In[4]:=
{"a", "b", "c", "d", "e"} // ResourceFunction["PhraseRiffle"][" and also "]
Out[4]=

Scope (4) 

If the list of strings is empty, the result is an empty string:

In[5]:=
ResourceFunction["PhraseRiffle"][{}] === ""
Out[5]=

If there is single string in the list, it is returned unchanged:

In[6]:=
ResourceFunction["PhraseRiffle"][{"hello"}]
Out[6]=

Create an operator form with a custom separator:

In[7]:=
phraser = ResourceFunction["PhraseRiffle"][" e ", "; "]
Out[7]=

Use it on a list:

In[8]:=
{"uno", "due", "tre", "quattro"} // phraser
Out[8]=

Include a serial comma:

In[9]:=
ResourceFunction["PhraseRiffle"][{"a", "b", "c", "d", "e"}, ", and "]
Out[9]=

Applications (1) 

Make a phrase stating the planets in the solar system:

In[10]:=
ResourceFunction["PhraseRiffle"][
 TextString /@ EntityList[EntityClass["Planet", All]]]
Out[10]=

Properties and Relations (1) 

PhraseRiffle is similar to the StringRiffle function but with the ability to customize the last separator. It is in fact implemented as two StringRiffle operations:

In[11]:=
ResourceFunction["PhraseRiffle"][{"a", "b", "c"}]
Out[11]=
In[12]:=
StringRiffle[{StringRiffle[{"a", "b"}, ", "], "c"}, " and "]
Out[12]=

Possible Issues (2) 

Spaces around your custom separator are not added automatically:

In[13]:=
ResourceFunction[
 "PhraseRiffle"][{"un", "deux", "trois", "quatre"}, "et"]
Out[13]=

Add the spaces for the correct result:

In[14]:=
ResourceFunction[
 "PhraseRiffle"][{"un", "deux", "trois", "quatre"}, " et "]
Out[14]=

Neat Examples (1) 

Count up to ten:

In[15]:=
Column@Map[Range/*Map[IntegerName]/*ResourceFunction["PhraseRiffle"], Range[10]]
Out[15]=

Publisher

GustavoDelfino

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 07 June 2024

Source Metadata

License Information