StringReplace
replaces the string expression s by sp wherever it appears in "string".
replaces the string expressions si by spi whenever they appear as substrings of "string".
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.
- 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 allclose allBasic Examples (3)Summary of the most common use cases
Replace every occurrence of "ab" with "X":

https://wolfram.com/xid/0cg3cywhe-niw

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

https://wolfram.com/xid/0cg3cywhe-emsetg

Use the operator form of StringReplace:

https://wolfram.com/xid/0cg3cywhe-nn0vlj

Scope (12)Survey of the scope of standard use cases

https://wolfram.com/xid/0cg3cywhe-4hkqvt


https://wolfram.com/xid/0cg3cywhe-gqwgs3


https://wolfram.com/xid/0cg3cywhe-vq6uak

Use pattern matching for dates:

https://wolfram.com/xid/0cg3cywhe-eg3o9g

Mixed regular expressions and string patterns:

https://wolfram.com/xid/0cg3cywhe-w8gzod

Use RuleDelayed (:>) in order to evaluate the right-hand side of a rule:

https://wolfram.com/xid/0cg3cywhe-clnwcm

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

https://wolfram.com/xid/0cg3cywhe-0cnztn

Replace occurrences of either string:

https://wolfram.com/xid/0cg3cywhe-bhpvcm

StringReplace works on special characters:

https://wolfram.com/xid/0cg3cywhe-3c7un

StringReplace automatically threads over lists of strings:

https://wolfram.com/xid/0cg3cywhe-hz99ah

Change the nucleotides in a biomolecular sequence:

https://wolfram.com/xid/0cg3cywhe-vufa0m

Use a wildcard in the pattern replaced in a given biomolecular sequence:

https://wolfram.com/xid/0cg3cywhe-m53evk

The "N" is a degenerate letter and is not a wildcard except in biomolecular sequences:

https://wolfram.com/xid/0cg3cywhe-huxwlp

Additional wraparound replacements may be made in circular biomolecular sequences:

https://wolfram.com/xid/0cg3cywhe-301d1j

Replace only literal degenerate letter occurrences using Verbatim:

https://wolfram.com/xid/0cg3cywhe-gp3xwp

Generalizations & Extensions (1)Generalized and extended use cases
Options (1)Common values & functionality for each option
Applications (7)Sample problems that can be solved with this function
Remove leading and trailing spaces:

https://wolfram.com/xid/0cg3cywhe-dtiu3h


https://wolfram.com/xid/0cg3cywhe-g8a2wh


https://wolfram.com/xid/0cg3cywhe-kq5vif

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

https://wolfram.com/xid/0cg3cywhe-buuqcy

Capitalize all words in a sentence:

https://wolfram.com/xid/0cg3cywhe-due0eu

Change the date convention in a text:

https://wolfram.com/xid/0cg3cywhe-dgsurp

Identify prime numbers in a text:

https://wolfram.com/xid/0cg3cywhe-4okuv1

A neighbor-independent substitution system:

https://wolfram.com/xid/0cg3cywhe-g95l9p

https://wolfram.com/xid/0cg3cywhe-6lzbc9

Properties & Relations (1)Properties of the function, and connections to other functions
StringPosition gives the starting and ending positions at which a pattern occurs:

https://wolfram.com/xid/0cg3cywhe-pxr3ip

The output can be used with StringReplacePart:

https://wolfram.com/xid/0cg3cywhe-fobf0i


https://wolfram.com/xid/0cg3cywhe-prwuv0

Wolfram Research (1991), StringReplace, Wolfram Language function, https://reference.wolfram.com/language/ref/StringReplace.html (updated 2020).
Text
Wolfram Research (1991), StringReplace, Wolfram Language function, https://reference.wolfram.com/language/ref/StringReplace.html (updated 2020).
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.
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
Wolfram Language. (1991). StringReplace. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/StringReplace.html
BibTeX
@misc{reference.wolfram_2025_stringreplace, author="Wolfram Research", title="{StringReplace}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/StringReplace.html}", note=[Accessed: 25-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_stringreplace, organization={Wolfram Research}, title={StringReplace}, year={2020}, url={https://reference.wolfram.com/language/ref/StringReplace.html}, note=[Accessed: 25-March-2025
]}