This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)


StringReplace or StringReplace
replaces the string expressions by whenever they appear as substrings of .
does only the first n replacements.
gives the list of results for each of the .
  • The string expressions 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.
  • In replacements of the form , the are not evaluated until each time they are used.
Replace every occurrence of "ab" with "X":
Replace every substring that has one or more occurrences of "ab" with "X":
Replace every occurrence of "ab" with "X":
Click for copyable input
Replace every substring that has one or more occurrences of "ab" with "X":
Click for copyable input
Use string patterns:
Use a regular expression:
Use pattern matching for dates:
Mixed regular expressions and string patterns:
Use RuleDelayed () in order to evaluate the right-hand side of a rule:
Replace only the two first occurrences that match the given pattern:
Replace occurrences of either string:
StringReplace works on special characters:
StringReplace automatically threads over lists of strings:
Replacements can be nonstrings:
This replaces all occurrences regardless of case:
Remove leading and trailing spaces:
Remove comments in a text:
Extract comments from a text:
Strip off all tags "<...>" from an HTML document:
Capitalize all words in a sentence:
Change the date convention in a text:
Identify prime numbers in a text:
A neighbor-independent substitution system:
Five steps in the evolution:
StringPosition gives the starting and ending positions at which a pattern occurs:
The output can be used with StringReplacePart:
Same result:
Highlight words of a given length in a text excerpt from the book A New Kind of Science:
New in 2 | Last modified in 5.1