bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java String replaceAll() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java String replaceAll() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ myStr = "I love cats. Cats are very easy to love. Cats are very popular.";
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The replaceAll() method replaces all the matches of a regular expression in a string with a new substring.
Definition and Usage
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