Flash cards
Review the key moves
1/4
Core idea
What is the main idea behind Java String replaceFirst() 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 = "This is ExampleSite";3Order
Put the learning moves in the order that makes the concept easiest to apply.
The replaceFirst() method replaces the first match of a regular expression in a string with a new substring.
Definition and Usage
Java String replaceFirst() Method
❮ String Methods
Example
String myStr = "This is ExampleSite";
String regex = "is";
System.out.println(myStr.replaceFirst(regex, "at"));Definition and Usage
The replaceFirst() method replaces the first match 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 replaceFirst(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 the first match. |
Technical Details
| Returns: | A copy of the string in which the first substring that matches the regular expression is replaced with a new substring. |
|---|---|
| Throws: | PatternSyntaxException - If the syntax of the regular expression is incorrect. |
| Java version: | 1.4 |