❮ 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
| Parameter | Description |
|---|---|
| regex | Required. A regular expression defining what substrings to search for. |
| replacement | Required. 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 |