({{, (RefLink[StringReplace, paclet:ref/StringReplace])["string",s->sp]
replaces the string expression s by sp wherever it appears in "string". }, {, (RefLink[StringReplace, paclet:ref/StringReplace])["string",{(Subscript[s, 1])->(Subscript[sp, 1]),(Subscript[s, 2])->(Subscript[sp, 2]),…}]
replaces the string expressions (Subscript[s, i]) by (Subscript[sp, i]) whenever they appear as substrings of "string".}, {, (RefLink[StringReplace, paclet:ref/StringReplace])["string",srules,n]
does only the first n replacements. }, {, (RefLink[StringReplace, paclet:ref/StringReplace])[{(Subscript[s, 1]),(Subscript[s, 2]),…},srules]
gives the list of results for each of the (Subscript[s, i]). }, {, (RefLink[StringReplace, paclet:ref/StringReplace])[srules]
represents an operator form of (RefLink[StringReplace, paclet:ref/StringReplace]) that can be applied to an expression.}})

OpenersNotesSection(Spacer1)Details and Options

The string expressions (Subscript[s, i]) can contain any of the objects specified in the notes for (RefLink[StringExpression, paclet:ref/StringExpression]).

(RefLink[StringReplace, paclet:ref/StringReplace]) goes through a string, testing substrings that start at each successive character position. On each substring, it tries in turn each of the transformation rules you have specified. If any of the rules apply, it replaces the substring, then continues to go through the string, starting at the character position after the end of the substring.

If the (Subscript[sp, i]) in the replacements (Subscript[s, i])->(Subscript[sp, i]) do not evaluate to strings, (RefLink[StringReplace, paclet:ref/StringReplace]) will yield a (RefLink[StringExpression, paclet:ref/StringExpression]) rather than an ordinary string.

In replacements of the form (Subscript[s, i]):>(Subscript[sp, i]), the (Subscript[sp, i]) are not evaluated until each time they are used.

Setting the option (RefLink[IgnoreCase, paclet:ref/IgnoreCase])->(RefLink[True, paclet:ref/True]) makes (RefLink[StringReplace, paclet:ref/StringReplace]) treat lowercase and uppercase letters as equivalent.

(RefLink[StringReplace, paclet:ref/StringReplace])[srules][expr] is equivalent to (RefLink[StringReplace, paclet:ref/StringReplace])[expr,srules].

OpenersPrimaryExamplesSection(Spacer1)Examples  (23)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Replace every occurrence of "ab" with "X":

StringReplace["abbaabbaa","ab"->"X"]

Replace every substring that has one or more occurrences of "ab" with "X":

StringReplace["ababbabbaaababa","ab"..->"X"]

Use the operator form of (RefLink[StringReplace, paclet:ref/StringReplace]):

StringReplace["ab"..->"X"]["ababbabbaaababa"]

OpenersExampleSection1(Spacer1)Scope  (9)

Use string patterns:

StringReplace["abc abcb abdc","ab"~~_->"X"]

StringReplace["abc abcd abcd",WordBoundary~~"abc"~~WordBoundary->"XX"]

Use a regular expression:

StringReplace["abcd acbd",RegularExpression["[ab]"]->"XX"]

Use pattern matching for dates:

StringReplace["abc 31/12/2003 abcd 5/3/2004 abc",DatePattern[{"Day","Month","Year"}]->"XX"]

Mixed regular expressions and string patterns:

StringReplace["abcd acbd",RegularExpression["[ab]"]~~_->"YY"]

Use (RefLink[RuleDelayed, paclet:ref/RuleDelayed]) (:>) in order to evaluate the right-hand side of a rule:

StringReplace["101abcd102abcd",a:DigitCharacter..:>ToString[ToExpression[a]^2]]

Replace only the two first occurrences that match the given pattern:

StringReplace["abcddbbcbbbacbbaa","bb" -> "X",2]

Replace occurrences of either string:

StringReplace["abcdabcdaabcabcd",{"abc"->"Y","d"->"XXX"}]

(RefLink[StringReplace, paclet:ref/StringReplace]) works on special characters:

StringReplace["product: A ⊕ B" ,"⊕"->"x"]

(RefLink[StringReplace, paclet:ref/StringReplace]) automatically threads over lists of strings:

StringReplace[{"aaabbbbaaaa","bbbaaaab","aaabab"},"ab"->"X"]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

Replacements can be nonstrings:

StringReplace["Expand %1 variables %13 to nonstring %a expressions %27","%"~~x:DigitCharacter..:>Var[ToExpression[x]]]

OpenersExampleSection3(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)IgnoreCase  (1)

This replaces all occurrences regardless of case:

StringReplace["The cat in the hat.","the"->"a",IgnoreCase->True]

OpenersExampleSection4(Spacer1)Applications  (7)

Remove leading and trailing spaces:

StringReplace["  Have a nice day.  ", (StartOfString ~~Whitespace) | (Whitespace ~~ EndOfString) -> ""]//FullForm

Remove comments in a text:

StringReplace["this (*comment1*) is a test (*comment2*)","(*"~~ShortestMatch[___]~~"*)"->""]

Extract comments from a text:

StringCases["this (*comment1*) is a test (*comment2*)","(*"~~ShortestMatch[___]~~"*)"]

Strip off all tags "<...>" from an HTML document:

StringReplace[
  "<title>The Title</title>
<h1>The <a href='link'>head</a></h1>
<p>Some text follows here...</p>","<"~~Except[">"]..~~">"->""]

Capitalize all words in a sentence:

StringReplace["this is a test",WordBoundary~~x_:>ToUpperCase[x]]

Change the date convention in a text:

StringReplace["abc 31/12/2003 abcd 5/3/2004 abc",a:DatePattern[{"Day","Month","Year"}]:>DateString[DateList[{a,{"Day","Month","Year"}}],{"Year","/","Month","/","Day"}]]

Identify prime numbers in a text:

StringReplace["I have 13 licenses: 6 front end licenses and 7 kernel licenses",
WordBoundary~~x:DigitCharacter..~~WordBoundary/;PrimeQ[ToExpression[x]]:>
"*"<>x<>"*"]

A neighbor-independent substitution system:

ssevolveList[rule_,init_String,t_Integer]:=NestList[StringReplace[#,rule]&,init,t]

Five steps in the evolution:

ssevolveList[ {"B"->"BA","A"->"AB"},"B",5]

OpenersExampleSection5(Spacer1)Properties & Relations  (1)

(RefLink[StringPosition, paclet:ref/StringPosition]) gives the starting and ending positions at which a pattern occurs:

StringPosition["abbaabbaa","ab"]

The output can be used with (RefLink[StringReplacePart, paclet:ref/StringReplacePart]):

StringReplacePart["abbaabbaa","X",%]

Same result:

StringReplace["abbaabbaa","ab"->"X"]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

Highlight words of a given length in a text excerpt from the book A New Kind of Science:

text="Just over twenty years ago I made what at first seemed like a small discovery: a computer experiment of mine showed something I did not expect. But the more I investigated, the more I realized that what I had seen was the beginning of a crack in the very foundations of existing science, and a first clue towards a whole new kind of science. This book is the culmination of nearly twenty years of work that I have done to develop that new kind of science. I had never expected it would take anything like as long, but I have discovered vastly more than I ever thought possible, and in fact what I have done now touches almost every existing area of science, and quite a bit besides.";

Manipulate[TextCell[Row[List@@StringReplace[text,{RegularExpression["\\b\\w{"<>ToString[u]<>"}\\b"]:>Style["$0",18,Bold]}]],"TR"],{u,1,10,1},SaveDefinitions->True]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Replace, paclet:ref/Replace])  ▪  (RefLink[StringReplaceList, paclet:ref/StringReplaceList])  ▪  (RefLink[StringReplacePart, paclet:ref/StringReplacePart])  ▪  (RefLink[StringDelete, paclet:ref/StringDelete])  ▪  (RefLink[StringPosition, paclet:ref/StringPosition])  ▪  (RefLink[StringTrim, paclet:ref/StringTrim])  ▪  (RefLink[StringSplit, paclet:ref/StringSplit])  ▪  (RefLink[StringTemplate, paclet:ref/StringTemplate])  ▪  (RefLink[ToLowerCase, paclet:ref/ToLowerCase])  ▪  (RefLink[ToUpperCase, paclet:ref/ToUpperCase])  ▪  (RefLink[RemoveDiacritics, paclet:ref/RemoveDiacritics])  ▪  (RefLink[Pluralize, paclet:ref/Pluralize])  ▪  (RefLink[SubstitutionSystem, paclet:ref/SubstitutionSystem])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Operations on Strings, paclet:tutorial/OperationsOnStrings])}, {▪(RefLinkPlain[Special Characters, paclet:tutorial/SpecialCharacters-StringsAndCharacters])}, {▪(RefLinkPlain[String Patterns, paclet:tutorial/StringPatterns])}, {▪(RefLinkPlain[Working with String Patterns, paclet:tutorial/WorkingWithStringPatternsOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[String Manipulation, paclet:guide/StringManipulation])}, {▪(RefLinkPlain[String Operations, paclet:guide/StringOperations])}, {▪(RefLinkPlain[Computational Systems, paclet:guide/ComputationalSystemsAndDiscovery])}, {▪(RefLinkPlain[Text Normalization, paclet:guide/TextNormalization])}, {▪(RefLinkPlain[Iterated Maps & Fractals, paclet:guide/IteratedMapsAndFractals])}, {▪(RefLinkPlain[Language Overview, paclet:guide/LanguageOverview])}, {▪(RefLinkPlain[Text Manipulation, paclet:guide/ProcessingTextualData])}, {▪(RefLinkPlain[String Patterns, paclet:guide/StringPatterns])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-FastIntro.png(Spacer1)Fast Introduction for Programmers: Strings, http://www.wolfram.com/language/fast-introduction-for-programmers/strings/])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])(WebLink[: String Patterns and Templates , https://www.wolfram.com/language/elementary-introduction/42-string-patterns-and-templates.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=StringReplace]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})