bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java String replaceAll() Method

❮ String Methods

Example

String myStr = "I love cats. Cats are very easy to love. Cats are very popular.";
String regex = "(?i)cat";
System.out.println(myStr.replaceAll(regex, "dog"));

Definition and Usage

The replaceAll() method replaces all the matches of a regular expression in a string with a new substring.

Tip

See the Java RegEx tutorial to learn about regular expressions.

Syntax

public String replaceAll(String
regex , String
replacement )

Parameter Values

ParameterDescription
regexRequired. A regular expression defining what substrings to search for.
replacementRequired. The substring which will replace each match.

Technical Details

Returns:A copy of the string in which matches of the regular expression are replaced with new substrings.
Throws:PatternSyntaxException - If the syntax of the regular expression is incorrect.
Java version:1.4

Previous

Java String replace() Method

Next

Java String replaceFirst() Method