Method - String replaceFirst(String regex, String replacement)

This method replaces the first substring of the subject string, which matches the given regular expression with the replacement String. As the name implies, replaceFirst replaces only the first matching occurrence of the input String. Note that we can also supply a simple String containing no regex meta character in the first argument as regex.

Calling this method is equivalent to the following call:

Pattern.compile(regex).matcher(input).replaceFirst(replacement); 
Note: We will cover the Pattern and Matcher APIs in later chapters.

A replacement string allows the use of group references, such as $1, $2, $3, and so on, for numbered references or ${groupName} for named references, which represent each of the captured groups in the regular expressions. To place a literal dollar or a literal backslash in the replacement, we need to escape these characters using the double backslash.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.16.81