bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

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

ParameterDescription
regexRequired. A regular expression defining what substrings to search for.
replacementRequired. 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

Previous

Java String replaceAll() Method

Next

Java String split() Method