Function Repository Resource:

StringReplaceRow

Source Notebook

Replace parts of strings with expressions to create a row

Contributed by: Bob Sandheinrich

ResourceFunction["StringReplaceRow"]["string",sexpr]

gives a row wherein the string expression s is replaced by expr wherever it appears in "string".

ResourceFunction["StringReplaceRow"]["string",{s1expr1,s2expr2,}]

replaces the string expressions si by expri whenever they appear as substrings of "string".

ResourceFunction["StringReplaceRow"]["string",rules,n]

does only the first n replacements.

ResourceFunction["StringReplaceRow"][{s1,s2,},rules]

gives the list of results for each of the si.

ResourceFunction["StringReplaceRow"][rules]

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

Details and Options

ResourceFunction["StringReplaceRow"] supports the same inputs and options as StringReplace.
When the rules match at least one part of "string", the result of ResourceFunction["StringReplaceRow"] is a Row. Otherwise, the original "string" is returned.

Examples

Basic Examples (4) 

Style a word within a string:

In[1]:=
ResourceFunction[
 "StringReplaceRow"]["find the mispelled word", {"mispelled" -> Style["mispelled", Red, 22]}]
Out[1]=

Replace "image" with an image:

In[2]:=
ResourceFunction[
 "StringReplaceRow"]["Computer, please make my image cooler", {"image" -> ResourceData["Sample Image: Orange Butterfly on a Purple Flower"]}]
Out[2]=

Use RuleDelayed to apply a function to the replaced string:

In[3]:=
ResourceFunction[
 "StringReplaceRow"]["Numbers that are divisible by 5 include: 20, 100, and 12345", {n : (DigitCharacter ..) :> Button[n, Print[ToExpression[n]/5]]}]
Out[3]=

Replace parts of multiple strings to create a musical interface:

In[4]:=
Column@ResourceFunction[
  "StringReplaceRow"][{"abcdefg", "gfedcba", "cefgcefgcefgeced"}, l : LetterCharacter :> Mouseover[l, Dynamic[EmitSound[Sound[SoundNote[l]]]; l]]]
Out[4]=

Scope (3) 

Replace multiple parts:

In[5]:=
ResourceFunction[
 "StringReplaceRow"]["Some of my favorite colors are red, green and purple", {"red" -> Style["red", Red], "green" -> Style["green", Green], "purple" -> Style["purple", Purple]}
 ]
Out[5]=

Create an operator to fix branding on company materials:

In[6]:=
op = ResourceFunction["StringReplaceRow"][
  "ACME CORP" -> Style["ACME CORP", Italic, Darker[Green]], IgnoreCase -> True]
Out[6]=

Apply the operator to make sure your corporate messaging is perfect:

In[7]:=
op["Here at ACME CORP we take safety seriously. We seriously apologize for any pain ACME CORP caused to the kind people of Central Florida. Be assured that ACME CORP is working hard to capture the mutant alligators.\n\nSincerely,\nACME CORP"]
Out[7]=

Insert computable entities into text:

In[8]:=
text = "Athens, St. Louis and London were the first modern Olympics host cities.";
In[9]:=
ResourceFunction["StringReplaceRow"][text, Rule @@@ TextCases[text, "City" -> {"String", "Interpretation"}]]
Out[9]=

Insert tooltips into text:

In[10]:=
ResourceFunction[
 "StringReplaceRow"]["The best hitter of the 2000s was Albert Pujols", {"Albert Pujols" -> Tooltip["Albert Pujols", ResourceFunction["FirstWebImage"]["Albert Pujols 2005"]]}]
Out[10]=

Applications (1) 

Create a function to help choose synonyms while writing:

In[11]:=
synonymPicker[str_, word_] := ResourceFunction["StringReplaceRow"][str, word -> PopupMenu[Dynamic[syn], Prepend[Synonyms[word], word], 1, Dynamic[syn], MenuStyle -> "Text"]]
In[12]:=
synonymPicker["Sometimes it is difficult to choose the right word", "difficult"]
Out[12]=

Properties and Relations (2) 

When replacements are made, the result is a Row:

In[13]:=
ResourceFunction["StringReplaceRow"][
  "aaabaaa", {"b" -> Range[5]}] // InputForm
Out[13]=

When no matches exist, the original string is returned:

In[14]:=
ResourceFunction["StringReplaceRow"][
  "aaabaaa", {"c" -> Range[5]}] // InputForm
Out[14]=

For results longer than the window size, the output is likely to wrap at the replacements:

In[15]:=
ResourceFunction["StringReplaceRow"][ToString[RandomInteger[10^10^3]],
  "00" -> Style["ZEROS", Pink, 10]]
Out[15]=

Neat Examples (2) 

Any part of a string can be bird said:

In[16]:=
ResourceFunction["StringReplaceRow"][
 WikipediaData["Birdsong", "SummaryPlaintext"], {word : ("songs" | "calls" | "sounds") :> ResourceFunction["BirdSay"][word]}]
Out[16]=

Words can also be bobbed:

In[17]:=
ResourceFunction["StringReplaceRow"][
 WikipediaData["Bob_(physics)", "SummaryPlaintext"], "bob" -> ResourceFunction["Bob"]["bob"]]
Out[17]=

Version History

  • 1.0.0 – 13 May 2020

Related Resources

License Information