StringReplace["string",ssp]
replaces the string expression s by sp wherever it appears in "string".
StringReplace["string",{s1sp1,s2sp2,…}]
replaces the string expressions si by spi whenever they appear as substrings of "string".
StringReplace["string",srules,n]
does only the first n replacements.
StringReplace[{s1,s2,…},srules]
gives the list of results for each of the si.
StringReplace[srules]
represents an operator form of StringReplace that can be applied to an expression.
StringReplace
StringReplace["string",ssp]
replaces the string expression s by sp wherever it appears in "string".
StringReplace["string",{s1sp1,s2sp2,…}]
replaces the string expressions si by spi whenever they appear as substrings of "string".
StringReplace["string",srules,n]
does only the first n replacements.
StringReplace[{s1,s2,…},srules]
gives the list of results for each of the si.
StringReplace[srules]
represents an operator form of StringReplace that can be applied to an expression.
Details and Options
- The string expressions si can contain any of the objects specified in the notes for StringExpression.
- 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 spi in the replacements si->spi do not evaluate to strings, StringReplace will yield a StringExpression rather than an ordinary string.
- In replacements of the form si:>spi, the spi are not evaluated until each time they are used.
- StringReplace takes the following option:
-
IgnoreCase False whether to turn off case-sensitive matching - Setting the option IgnoreCase->True makes StringReplace treat lowercase and uppercase letters as equivalent.
- StringReplace[srules][expr] is equivalent to StringReplace[expr,srules].
- StringReplace[BioSequence["type","seq"],srules,…] will replace occurrences in the string "seq" according to srules yielding a biomolecular sequence. In this case, degenerate letters in the si of the rules si->spi are interpreted as wildcard patterns based on the type of biomolecular sequence. Use Verbatim[si] to match degenerate letters literally.
- The documentation for BioSequence lists the degenerate letters supported by each type of biomolecular sequence.
- If the biomolecular sequence operated upon by StringReplace is circular, wraparound matches are possible.
Examples
open all close allBasic 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 StringReplace:
StringReplace["ab".. -> "X"]["ababbabbaaababa"]Scope (12)
StringReplace["abc abcb abdc", "ab" ~~ _ -> "X"]StringReplace["abc abcd abcd", WordBoundary ~~ "abc" ~~ WordBoundary -> "XX"]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 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"}]StringReplace works on special characters:
StringReplace["product: A ⊕ B" , "⊕" -> "x"]StringReplace automatically threads over lists of strings:
StringReplace[{"aaabbbbaaaa", "bbbaaaab", "aaabab"}, "ab" -> "X"]Change the nucleotides in a biomolecular sequence:
StringReplace[BioSequence["DNA", "ATGGCGGT"], "GG" -> "TC"]Use a wildcard in the pattern replaced in a given biomolecular sequence:
StringReplace[BioSequence["DNA", "GTTGGCTTG"], "GGN" -> "A"]The "N" is a degenerate letter and is not a wildcard except in biomolecular sequences:
StringReplace["GTTGGCTTG", "GGN" -> "A"]Additional wraparound replacements may be made in circular biomolecular sequences:
StringReplace[BioSequence["CircularDNA", "GTTGGCTTG"], "GGN" -> "A"]Replace only literal degenerate letter occurrences using Verbatim:
StringReplace[BioSequence["DNA", "GTTGGCTTG"], "GG" ~~ Verbatim["N"] -> "A"]Generalizations & Extensions (1)
Options (1)
Applications (7)
Remove leading and trailing spaces:
StringReplace[" Have a nice day. ", (StartOfString ~~ Whitespace) | (Whitespace ~~ EndOfString) -> ""]//FullFormStringReplace["this (*comment1*) is a test (*comment2*)", "(*" ~~ Shortest[___] ~~ "*)" -> ""]StringCases["this (*comment1*) is a test (*comment2*)", "(*" ~~ Shortest[___] ~~ "*)"]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]ssevolveList[ {"B" -> "BA", "A" -> "AB"}, "B", 5]Properties & Relations (1)
StringPosition gives the starting and ending positions at which a pattern occurs:
StringPosition["abbaabbaa", "ab"]The output can be used with StringReplacePart:
StringReplacePart["abbaabbaa", "X", %]StringReplace["abbaabbaa", "ab" -> "X"]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]Tech Notes
History
Introduced in 1991 (2.0) | Updated in 1999 (4.0) ▪ 2000 (4.1) ▪ 2002 (4.2) ▪ 2003 (5.0) ▪ 2004 (5.1) ▪ 2016 (10.4) ▪ 2020 (12.2)
Text
Wolfram Research (1991), StringReplace, Wolfram Language function, https://reference.wolfram.com/language/ref/StringReplace.html (updated 2020).
CMS
Wolfram Language. 1991. "StringReplace." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/StringReplace.html.
APA
Wolfram Language. (1991). StringReplace. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringReplace.html
BibTeX
@misc{reference.wolfram_2026_stringreplace, author="Wolfram Research", title="{StringReplace}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringReplace.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_stringreplace, organization={Wolfram Research}, title={StringReplace}, year={2020}, url={https://reference.wolfram.com/language/ref/StringReplace.html}, note=[Accessed: 13-June-2026]}