❮ 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 |